Unix Shell Scripting Interview Questions

______________________________________________________________

Thursday, November 29, 2007

Unix Shell Scripting Interview Questions - 1
______________________________________________________________

• How do you find out what’s your shell? - echo $SHELL

• What’s the command to find out today’s date? - date

• What’s the command to find out users on the system? - who

• How do you find out the current directory you’re in? - pwd

• How do you remove a file? - rm

• How do you remove a - rm -rf

• How do you find out your own username? - whoami

• How do you send a mail message to somebody? - mail somebody@mindgrillq.com -s ‘Your
subject’ -c ‘cc@mindgrillq.com


• How do you count words, lines and characters in a file? - wc

• How do you search for a string inside a given file? - grep string filename

• How do you search for a string inside a directory? - grep string *

• How do you search for a string in a directory with the subdirectories recursed? - grep -r string
*

• What are PIDs? - They are process IDs given to processes. A PID can vary from 0 to 65535.

• How do you list currently running process? - ps

• How do you stop a process? - kill pid

• How do you find out about all running processes? - ps -ag

• How do you stop all the processes, except the shell window? - kill 0

• How do you fire a process in the background? - ./process-name &

• How do you refer to the arguments passed to a shell script? - $1, $2 and so on. $0 is your script
name.

• What’s the conditional statement in shell scripting? - if {condition} then … fi

• How do you do number comparison in shell scripts? - -eq, -ne, -lt, -le, -gt, -ge

• How do you test for file properties in shell scripts? -

-s filename tells you if the file is not empty,

-f filename tells you whether the argument is a file, and not a directory,

-d filename tests if the argument is a directory, and not a file,

-w filename tests for writeability,

-r filename tests for readability,

-x filename tests for executability

• How do you do Boolean logic operators in shell scripting?

- ! tests for logical not,

-a tests for logical and, and

-o tests for logical or.

• How do you find out the number of arguments passed to the shell script? - $#

• What’s a way to do multilevel if-else’s in shell scripting?

- if {condition} then {statement} elif {condition} {statement} fi

• How do you write a for loop in shell? - for {variable name} in {list} do {statement} done

• How do you write a while loop in shell? - while {condition} do {statement} done

• How does a case statement look in shell scripts?

- case {variable} in {possible-value-1}) {statement};; {possible-value-2}) {statement};; esac

• How do you read keyboard input in shell scripts? - read {variable-name}

• How do you define a function in a shell script? - function-name() { #some code here return }

• How does getopts command work?

- The parameters to your script can be passed as -n 15 -x 20. Inside the script, you can iterate
through the getopts array as while getopts n:x option, and the variable $option contains the
value of the entered option.

5 Response to "Unix Shell Scripting Interview Questions"

  1. Anonymous says:

    Thanks Venky..... Information given by you is very useful :)

    According to me info are OK but there should be more questions and answers. these are very common questions.

    Anonymous says:

    Thanks...

    Unknown says:

    Hi Mate,


    I am shocked, shocked, that there is such article exist!! But I really think you did a great job highlighting some of the key Tutorials in the entire space.

    I want get caption of textbox a program when clicked on it's button by c/c++;
    it is maybe ?
    Thank you very much and will look for more postings from you.


    Gracias,
    Banu

    Unknown says:

    Hello There,

    So bloody thorough! Ah! So happy and blissed out! I feel redeemed by reading out Unix Shell Scripting Interview Questions. Keep up the good work!

    Though the basic unit of a Unix and Unix-like systems include at least some BSD code, and some systems also include GNU utilities in their distributions. I've came across forums it says you can create your own mini OS using C/C++ compiler.
    Is that entirely true? How much time it takes to replicate an OS like Chrome OS


    Please keep providing such valuable information.

    Obrigado,
    Preethi

Post a Comment

Powered by Blogger