Saturday, 16 May 2015

Unix Shell Scripting Interview questions





1) Difference between the output of echo ** and echo * *
echo ** lists all the filenames in the current directory..
echo * * lists all the filenames in the current directory twice.

2) The other way of running shell script apart from using sh command and chmod?

A) using ! we can run a shell script

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

4. What’s the conditional statement in shell scripting?
A) - if {condition} then … fi

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

6) How do you test for file properties in shell scripts?
A) - -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 executable

7)  How do you do Boolean logic operators in shell scripting?
A) - ! tests for logical not, -a tests for logical and, and -o tests for logical or.

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

9) What’s a way to do multilevel if-else’s in shell scripting?
A) – if {condition} then {statement} elif {condition} {statement} fi

10) How do you write a for loop in shell?
A) -–for {variable name} in {list} do {statement} done

11)  How do you write a while loop in shell?
A) -–while {condition} do {statement} done

12)  How does a case statement look in shell scripts?
A. -–case {variable} in {possible-value-1}) {statement};; {possible-value-2})
      {statement};;  esac

13)  How do you read keyboard input in shell scripts?
 A) - read {variable-name}

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

15)  How does getopts command work?
 A)  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.

1 comment:

  1. Nice article very good and useful information you given thanks for sharing.
    unix shell scripting training

    ReplyDelete