70 Shell Scripting Interview Questions
70 Shell Scripting Interview Questions
We have selected expected 70 shell scripting question and answers for your interview preparation.
Its really vital for all system admin to know scripting or atleast the basics which in turn helps to
automate many tasks in your work environment. In the past few years we have seen that all linux job
specification requires scripting skills.
./show.sh file1.txt
cat show.sh
#!/bin/bash
cat $1
cat copy.sh
#!/bin/bash
cp $1 $2
9) How to get 2nd element from each line from a file, if first equal
FIND
awk '{ if ($1 == "FIND") print $2}'
Example
#!/bin/bash xv
Output
Hello+World
Output
3
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
27) Which is the symbol used for comments in bash shell scripting
?
#
30) How to redirect stdout and stderr streams to log.txt file from
script inside ?
Add "exec >log.txt 2>&1" as the first command in the script
31) How to get part of string variable with echo command only ?
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
34) How to list users which UID less that 100 (awk) ?
awk -F: '$3<100' /etc/passwd
35) Write the program which counts unique primary groups for
users and displays count and group name only
cat /etc/passwd|cut -d: -f4|sort|uniq -c|while read c g
do
{ echo $c; grep :$g: /etc/group|cut -d: -f1;}|xargs -n 2
done
49) Write the command which will print numbers from 0 to 100 and
display every third (0 3 6 9 ) ?
for i in {0..100..3}; do echo $i; done
or
for (( i=0; i<=100; i=i+3 )); do echo "Welcome $i times"; done