1. What is the first line in any script?
Shebang
#!/bin/bash
2. How to comment in script?
We put ‘#’ in-front of any line. It will be treated as a comment.
3. Write if statement?
If [ condition ]
then
Statements
else
statments
fi
E.g
If [ $var > 9 ]
then
echo “Given number is greater then 9”
else
echo “Given number is less then 9”
fi
4. Write for loop syntax?
for variable in we are testing for loop
do
statements
done
E.g
for var in *
do
if [ -f $var ]
then
echo $var
fi
done
5. Write while loop syntax?
While [ condition ]
do
statements
done
E.g
num=0
while [ $num –lt 10 ]
do
num=`expr $num + 1`
echo $num
done
6. Write case command syntax?
case var in
dog) echo “you have entered animal name”;;
whale) echo “you have entered fish name”;;
parrot) echo “you have entered bird name”;;
*) echo “you have entered wrong value”;;
esac
7. How to print line by line from a file?
for line in `cat file_name`
do
echo $line
sleep 1
done
8. How to sum each value of a row?
num
12345
6 7 8 9 10
11 12 13 14
Output --
SUM 1+2+3+4+5 = 15
SUM 6+7+8+9+10 = 40
SUM 11+12+13+14 = 50
Ans:
#!/bin/bash
exec < num
while read line
do
sum=`echo $line|tr -s ' ' '\n'|awk -F' ' '{x+=$1}END{print x}'`
line=`echo $line|tr -s ' ' '+'`
echo "SUM $line = $sum"
sleep 1
echo
done
9. Find all the users whose user id is greater than 99?
awk –F’:’ ‘{print $3}’ /etc/passwd > userid
for line in `cat userid`
do
if [ $line –gt 99 ]
then
awk -F’:’ ‘{print $1,$3}’ /etc/passwd | grep $line
fi
done
10. Difference between $* and $@?
$* will display all parameters together but $@ will display all the parameters separately.
11. What $? Will do?
It will show the exit status for last executed command
If it will return “0” then our command was successful, other than “0” means unsuccessful.
12. How do you refer the arguments passed to any shell script?
$1, $2 …….. maximum upto $9
If we want to use 10th argument or parameter then we will shift 1
Shift 1
echo $9 ( now 10th arguments will be treated as 9th argument)
13. What $0 will do?
It contains Script name
14. How do you do number comparison in shell script?
-gt, -lt , -le, -ge , -eq and -ne
15. How set permanent variable?
X=5
export x
echo $x
16. How to set local variables?
X=5
17. What $$ will do?
It will show the PID of current process
18. What $# will do?
It will show total number arguments supplied to shell script
19. Write a shell script to check if we passed 3 arguments or not. If we pass three arguments then
message should be like “you have entered 3 arguments” or else it will message “Please enter 3
arguments”
If [ $# -eq 3 ]
then
echo “You have entered three arguments”
else
echo “Please enter three arguments”
fi
20. How to convert .txt file to .c files?
for files in *.txt
do
prefix=`basename -s .txt $files `
mv $files ${prefix}.c
done
21. What $! Will do?
It will show PID for last background job
22. Create Fibonacci series.
Series=1
Previous=0
While [ $series -le 100 ]
do
echo $series
p1=$series
series=`expr $previous + $series`
previous=$p1
done
23. Create script to count all the files in below directory structure?
a/b/c/d
for var in a/b/c/d
do
cd $var
count=`ls –l | grep ‘^-‘ | wc -l`
echo “$var has $count files”
done
24. How to schedule a job in linux?
Using “crontab” command
Minutes hour day_of_month month day_of_week command
30 08 10 06 * ./script.sh
It will run the script.sh on 10th Jun at 08:30 AM
25. How to view the crontab entries?
Crontab -l
26. How to add new entry to schedule a job?
crontab –e ( it will open a file with “vi” editor and now you can add new entry and save that file )
27. How to remove entry from crontab?
crontab -r ( never ever use this command in company, it will remove all the crontab entries )
to remove only one entry
crontab –e ( it will open “vi” editor, then delete the line that you want to remove )
28. To schedule a job every minute?
* * * * * ./script.sh
29. To schedule a job for more than one time ?
00 11,16 * * * ./script.sh
It will run every day, every month at 11:00 AM and 04:00 PM .
30. How to debug the scripts?
sh -x scripts.sh
31. How to execute sql queries using shell scritps?
echo “ hello”
sqlplus -s username/password << EOF
select * from tab_name;
drop table tab_name;
exit;
EOF
echo “ End of the script”
32. How do you use non filter commands as filter command?
We will used “xargs” command
Like: if we want to delete all the links from current directory
ls –l | grep ‘^l’ | awk ‘{print $9 }’ | xargs rm
( as we know “rm” is not filter command, so now because of “xargs” , rm command is treated like filter
command )
33. How to check syntax error in shell scripting?
Sh -nv scriptname
“nv” option is used to check syntax error