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.