Following are some of the most frequently asked Shell Scripting interview questions in the interview, here are the answers to them.
Ans:
For loop:
for i in $( ls ); do
echo item: $i
done
While loop:
#!/bin/bash COUNTER=0 while [ $COUNTER -lt 10 ]; do echo The counter is $COUNTER let COUNTER=COUNTER+1 done
Until loop:
#!/bin/bash COUNTER=20 until [ $COUNTER -lt 10 ]; do echo COUNTER $COUNTER let COUNTER-=1 done
Ans: That line tells which shell to use. #!/bin/bash script to execute using /bin/bash. In case of python script there will be #!/usr/bin/python.
Ans: Makes variable public in subshells.
Ans: add "&" to the end of the script.
Ans: Makes script executable for script owner.
Want to acquire industry skills and gain complete knowledge of Shell Scripting? Enrol in Instructor-Led live UNIX Shell Scripting Training to get Job Ready! |
Ans: Redirects output stream to file or another stream.
Ans:
& - we use it when we want to put the script to the background
&& - when we want to execute a command/script if the first script was finished successfully
Ans: When we need to run several commands if the condition meets.
Ans: My name is $name
Ans: #
Ans: variable
Ans:
Ans: echo $* or echo $@
Ans: [ $a == $b ] - should be used for string comparison [ $a -eq $b ] - should be used for number tests
Ans:
Ans: [ $a -gt 12 ]
Ans: [ $b -le 12 ]
Ans: Add "exec >log.txt 2>&1" as the first command in the script.
Ans:
echo ${variable:x:y} x - start position y - length example: variable="My name is Petras, and I am developer." echo ${variable:11:6} # will display Petras
Ans:
echo ${variable#*:*:*:} or echo ${variable##*:}
Ans:
echo ${variable%:*:*:*} or echo ${variable%%:*}
Ans: awk -F: '$3<100' /etc/passwd.
Ans:
Operator | Syntax | Description |
eq | INTEGER1 -eq INTEGER2 | INTEGER1 is equal to INTEGER2 |
ge | INTEGER1 -ge INTEGER2 | INTEGER1 is greater than or equal to INTEGER2 |
gt | INTEGER1 -gt INTEGER2 | INTEGER1 is greater than INTEGER2 |
le | INTEGER1 -le INTEGER2 | INTEGER1 is less than or equal to INTEGER2 |
lt | INTEGER1 -lt INTEGER2 | INTEGER1 is less than INTEGER2 |
ne | INTEGER1 -ne INTEGER2 | INTEGER1 is not equal to INTEGER2 |
Ans: array=("Hi" "my" "name" "is")
Ans: echo ${array[0]}
Ans: echo ${array[@]}
Ans: echo ${!array[@]}
Ans: unset array[123]
Ans: array[1980]="New_element"
You liked the article?
Like: 0
Vote for difficulty
Current difficulty (Avg): Medium
TekSlate is the best online training provider in delivering world-class IT skills to individuals and corporates from all parts of the globe. We are proven experts in accumulating every need of an IT skills upgrade aspirant and have delivered excellent services. We aim to bring you all the essentials to learn and master new technologies in the market with our articles, blogs, and videos. Build your career success with us, enhancing most in-demand skills in the market.