Interview Q&A for Shell Script Part-I

Q1:- How can we pass argument to a script ?
 Ans:- At command line after script name we can pass arguments
./script argument

Example:- Script will show the content of file1.txt

./file.sh file1.txt

cat file.sh
#!/bin/bash
cat $1

Q2:- How to use argument in a script ? 
Ans:- To use argument we use $ with position of arguments
For First argument: $1
For Second argument : $2

Example : Script will copy myfile.txt (arg1) to destination (arg2)

cat copy.sh
#!/bin/bash
cp $1 $2

./copy.sh myfile.txt /home/oracle/
   
Q3:-How to get last 10 lines from a file ? 
Ans:- tail -10 file.txt

Q4:-How to get first 10 lines from a file ?
 Ans:-head -10 file.txt

Q5:-How to get 10th line from the text file ?
 Ans:- head -10 file.txt|tail -1

Q6:-How to get 3rd element from each line from a file ?
 Ans:- awk '{print $3}' file.txt

Q7:-How to get 2nd element from each line from a file, if first equal FIND ?
 Ans:-awk '{ if ($1 == "FIND") print $2}'

Q8:-How to debug bash script ? 
Ans:-Add -xv to #!/bin/bash
           OR
set -x       
           OR
Use -x option on execution of script

Example:-
#!/bin/bash –xv
            OR
#!/bin/bash
set -x
            OR
sh -x myscript.sh           

Q9:-how can we create function  ?
 Ans:-Below is the syntax for function

function function_name {
executable statement;
}

Example:-

function add()
{
a=$1;
b=$2;
sum=$((a+b));
echo "Sum:-$sum"
}

Q10:-How to add string to string ?
 Ans:-
V1="Hello"
V2="World"
let V3=$V1+$V2
echo $V3

Output
Hello+World



 
Q11:- How to add two integers ?
 Ans:-
V1=1
V2=2
let V3=$V1+$V2
echo $V3

Output
3

Remember you need to add "let" to line V3=$V1+$V2 then echo $V3 will give 3
if without let , then it will be echo $V3 will give 1+2

Q12:- How can we check the file exist on filesystem ? 
Ans:-
if [ -f /home/oracle/abc.txt ] then
echo "File exists"
fi

Q13:-Write down syntax for all loops in shell scripting ?
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

Q14:-What it means by #!/bin/sh or #!/bin/bash at beginning of every script ?
Ans:-That line tells which shell to use. #!/bin/bash script to execute using /bin/bash.
In case of python script there there will be #!/usr/bin/python

Q15:-What is the first symbol in the bash script file
Ans:-#

Q16:-What would be the output of command: [ -z "" ] && echo 0 || echo 1
Ans:- 0

Q17:-What command "export" do ? 
Ans:-Makes variable public in subshells

Q18:-How to run script in background ? 
Ans:-add "&" to the end of script

Q19:-What "chmod 500 script" do ?
 Ans:-Makes script read and executable for script owner only.

r - 4  (Read)
w - 2 (Write)
x - 1  (Execute)


Q20:-What ">" do ? 
Ans:-Redirects output stream to file or another stream.

1) ">" For redirect the output in overwrite mode
2) ">>" For redirect output in append mode
3) "<" For read mode

3 comments: