Interview Questions on Shell Script

26. How to write until loop?

until command
do
   Statement(s) to be executed until command is true
done

27. How to write select loop?

until command
do
   Statement(s) to be executed until command is true
done

29. What is the use of break?

At particular condion if we want to come out of loop then we can use break.

30. What is the use continue?

if we want to skip particular condition then we can use break.

Shell Script 26 - 30

Interview Questions on Shell Script

21. Write code for if statement in shell script?


Syntax :-
if [ expression 1 ]
then
   Statement(s) to be executed if expression 1 is true
   Statement(s) to be executed if no expression is true
fi

22. Write if else code in shell script?

Syntax :-
if [ expression 1 ]
then
   Statement(s) to be executed if expression 1 is true
elif [ expression 2 ]
then
   Statement(s) to be executed if expression 2 is true
else
   Statement(s) to be executed if no expression is true
fi

23. Write switch statement code in shellscript?

Syntax :- 
case word in
  pattern1)
     Statement(s) to be executed if pattern1 matches
     ;;
  pattern2)
     Statement(s) to be executed if pattern2 matches
     ;;
  pattern3)
     Statement(s) to be executed if pattern3 matches
     ;;
esac

24. How to write while loop in shelscript?

while command
do
   Statement(s) to be executed if command is true
done

25. How to write for loop?

for var in word1 word2 ... wordN
do
   Statement(s) to be executed for every word.
done

Shell Script 21 - 25

Interview Questions on Shell Script

16. How to know the exit status of last executed command?

Use $?

17. How to declare and initialize an array?

Syntax :- array_name[index]=value
Ex :- 
NAME[0]="Zara"
NAME[1]="Qadir"
NAME[2]="Mahnaz"
NAME[3]="Ayan"
NAME[4]="Daisy"

18. How to access array values?

Syntax :- array_name[index]
Ex :- 
FirstName = NAME[0];

19. How compare two variables?

-eq :- Checks if the value of two operands are equal or not, if yes then condition becomes true. 
Ex:-[ $a -eq $b ] is not true.
-ne :- Checks if the value of two operands are equal or not, if values are not equal then condition becomes true. 
Ex :- [ $a -ne $b ] is true. 

-gt :- Checks if the value of left operand is greater than the value of right operand, if yes then condition becomes true. 
Ex :- [ $a -gt $b ] is not true.

-lt :- Checks if the value of left operand is less than the value of right operand, if yes then condition becomes true. 
Ex :- [ $a -lt $b ] is true.

-ge :- Checks if the value of left operand is greater than or equal to the value of right operand, if yes then condition becomes true. 
Ex :- [ $a -ge $b ] is not true.
-le :- Checks if the value of left operand is less than or equal to the value of right operand, if yes then condition becomes true.
Ex :- [ $a -le $b ] is true.

20.  What are all the Boolean operators?
! This is logical negation. This inverts a true condition into false and vice versa. 
Ex :- [ ! false ] is true.
-o This is logical OR. If one of the operands is true then condition would be true. 
Ex :- [ $a -lt 20 -o $b -gt 100 ] is true.

-a This is logical AND. If both the operands are true then condition would be true otherwise it would be false. 
Ex :- [ $a -lt 20 -a $b -gt 100 ] is false.

Shell Script 16 - 20

Interview Questions on Shell Script

11. What is the use unset?

use unset
$ unset PI

12. What happens if we execute ‘echo $$’; ?

Prints procees id of current shell.

13. What is the use of $0?

The filename of the current script.

14. What is the use of $#?

The number of arguments supplied to a script.

15. What is the use of $*?

Prints all arguments.

Shell Script 11 - 15

Interview Questions on Shell Script

6. How to execute shell script files?

 sh filename.sh

7. What are the conditions to write an variable name?

syntax: 
bash    your-script-name
sh       your-script-name
./your-script-name

Examples : 
$ bash   bar
$ sh   bar
$ ./bar
8. What are all the types of variables?
1) System variables - Created and maintained by Linux itself. This type of variable defined in CAPITAL LETTERS. 
2) User defined variables (UDV) - Created and maintained by user. This type of variable defined in lower letters.

9. How to define User defined variables (UDV) ?

Syntax:  
variable name=value
NOTE: Here 'value' is assigned to given 'variable name' and Value must be on right side = sign.

10. How to set readonly variables?

Use readonly
  $ readonly PI=3.14

Shell Script 06 - 10

Interview Questions on Shell Script

1. What is Shell Script?

A shell script is a program file that runs unix commands based on the conditions for a UNIX-based operating system. 

2. What are the types of shell?

Two main types of shells:
The Bourne shell :-  If you are using a Bourne-type shell, the default prompt is the $ character.
The C shell :- If you are using a C-type shell, the default prompt is the % character.

3. Write sample program in shell script?

#!/bin/bash
clear 
echo  “Hello World"

4. How to write comments in shell script?

Start with # and then write comment lines.
Ex :- 
# Commnets goes here

5. What is the extension of shell script?

Generally we use .sh extension for shell sctipt.

Shell Script 01 - 05