SHELL SCRIPTING EXAMPLES-2
--------------------------------------------------------------------------------------------------------------------------
1). Write a script accept a month and display quarter of of given month?
echo -n "Enter a month[mon]:"
read mm
case $mm in
jan|feb|mar)echo "1st Quarter";;
apr|may|jun)echo "2nd Quarter";;
jul|aug|sep)echo "3rd Quarter";;
oct|nov|dec)echo "4th Quarter";;
*)echo "Invalid month.";;
esac
#[Jj]an|[Ff]eb|[Mm]ar)echo "1st Quarter";;
#[Aa][Uu][Gg]
#[Aa][Uu][Gg]*
--------------------------------------------------------------------------------------------------------------------------
2).#Write a script print no's from 1 to 10?
i=1
while [ $i -le 10]
do
echo $i
i='expr $i+1'
done
--------------------------------------------------------------------------------------------------------------------------
3).#Write a script accept a string and display reverse of the giving string?
echo -n "Enter a string :"
read str
l='echo $str | wc -c' #length of the string
while [ $l -gt 0 ]
do
ch='echo $str | cut -c $l'
temp=$temp$ch
l='expr $l -l'
done
echo "Reverse of $str is : $temp"
--------------------------------------------------------------------------------------------------------------------------
4).#Write a script accept the numbers and check whether the number is between 1 to 4?
echo "Enter a number b/w 1 to 4:"
read n
case $n in
1)echo "one";;
2)echo "two";;
3)echo "three";;
4)echo "four";;
*)echo "invalid number";;
esac
--------------------------------------------------------------------------------------------------------------------------
5).#Example of Menu program
clear
tput cup 6 10
echo "MAIN MENU"
tput cup 7 10
echo "********"
tput cup 8 10
echo "1.Date"
tput cup 9 10
echo "2.List of users"
tput cup 10 10
echo "3.Open a file"
tput cuo 11 10
echo "4.delete a file"
tput cup 12 10
echo "5.Exit"
tput cup 20 5
echo "enter a choice[1-5] : "
read choice
case $choice in
1)echo "Today date is : 'date'";;
2)who;;
3)sh foen.sh;; #./fopen.sh
4)sh del.sh;; #./del.sh
5)echo "Thank You"
exit;; #to terminate the program
*)echo "choice wrong. try again";;
esac
--------------------------------------------------------------------------------------------------------------------------
6).Example of while loop.
ans="y"
while [$ans = "y" ]
do
echo "Enter a filename to open:"
read fn
if [ -e $fn -a -f $fn ]
then
cat $fn
else
echo "no such file"
fi
echo "Do u want to open one more file [y/n] : "
read ans
done
-------------------------------------------------------------------------------------------------------------------------
7).Example of while loop.
while true # until false
do
echo "Enter a filename to open : "
read fn
if [ -e $fn -a -f $fn ]
then
cat $fn
break
else
continue
fi
done
--------------------------------------------------------------------------------------------------------------------------
8).Example of sleep
#sleep is used for to stop the execution specified no of seconds.
while true
do
clear
tput cup 5 8
echo "WELCOME TO"
sleep 2
clear
echo "TECNOSOFT"
sleep 2
done
--------------------------------------------------------------------------------------------------------------------------
9).#Write a script create " n" no of users
echo -n "Enter no of users to create : "
read n
$i=l
while [ $i -le $n ]
do
$x=tecno$i #it creates users with tecno name
useradd $x
i='expr $i+1'
done
--------------------------------------------------------------------------------------------------------------------------
10).#Example of for loop
for i in 1 2 3 4 5
do
echo $i
done
--------------------------------------------------------------------------------------------------------------------------
11).#Example of for loop
a=10
b=20
c=30
for i in a b c
do
echo $i
done
--------------------------------------------------------------------------------------------------------------------------
12).#Example of for loop
a=10
b=20
c=30
for i in $a $b $c
do
echo $i
done
--------------------------------------------------------------------------------------------------------------------------
13).#Write a script to display all sub directories of current directory
for i in *
do
if [ -d $i ]
then
echo $i
fi
done
--------------------------------------------------------------------------------------------------------------------------
14).#Write a script to display all empty files in the current directory
for i in *
do
if [ ! -s$i ]
then echo $i #rm $i =>to delete empty file
fi
done
--------------------------------------------------------------------------------------------------------------------------
15).#Write a script to display all exe files in the current directory
for i in*
do
if [ -f $i -a -r $i -a -x $i ]
then
echo $i
done
--------------------------------------------------------------------------------------------------------------------------
16).#Write a script to display details of employees who are receiving salary more than 5000 from emp file
#101,hari,9000,10
#102,madhu,4000,20
#103,anu,000,30
#104,priya,8000,10
for i in 'cat emp'
do
j='echo $i | cut -d"," -f -3'
then
echo $i
fi
done
--------------------------------------------------------------------------------------------------------------------------
17).#Write a script retrieve details of employees who are receiving salary more than 5000 in deptno 10 from emp file and insert into emp1 file
#101,hari,9000,10
#102,madhu,4000,20
#103,anu,666666000,30
#104,priya,8000,10
for i in 'cat emp'
do
j='echo $i | cut -d"," -f 3'
k='echo $i | cut -d ","-f 4'
if [ $j -ge 5000 -a $k -eq 10 ]
then
echo $i>>emp1
fi
done
--------------------------------------------------------------------------------------------------------------------------
1). Write a script accept a month and display quarter of of given month?
echo -n "Enter a month[mon]:"
read mm
case $mm in
jan|feb|mar)echo "1st Quarter";;
apr|may|jun)echo "2nd Quarter";;
jul|aug|sep)echo "3rd Quarter";;
oct|nov|dec)echo "4th Quarter";;
*)echo "Invalid month.";;
esac
#[Jj]an|[Ff]eb|[Mm]ar)echo "1st Quarter";;
#[Aa][Uu][Gg]
#[Aa][Uu][Gg]*
--------------------------------------------------------------------------------------------------------------------------
2).#Write a script print no's from 1 to 10?
i=1
while [ $i -le 10]
do
echo $i
i='expr $i+1'
done
--------------------------------------------------------------------------------------------------------------------------
3).#Write a script accept a string and display reverse of the giving string?
echo -n "Enter a string :"
read str
l='echo $str | wc -c' #length of the string
while [ $l -gt 0 ]
do
ch='echo $str | cut -c $l'
temp=$temp$ch
l='expr $l -l'
done
echo "Reverse of $str is : $temp"
--------------------------------------------------------------------------------------------------------------------------
4).#Write a script accept the numbers and check whether the number is between 1 to 4?
echo "Enter a number b/w 1 to 4:"
read n
case $n in
1)echo "one";;
2)echo "two";;
3)echo "three";;
4)echo "four";;
*)echo "invalid number";;
esac
--------------------------------------------------------------------------------------------------------------------------
5).#Example of Menu program
clear
tput cup 6 10
echo "MAIN MENU"
tput cup 7 10
echo "********"
tput cup 8 10
echo "1.Date"
tput cup 9 10
echo "2.List of users"
tput cup 10 10
echo "3.Open a file"
tput cuo 11 10
echo "4.delete a file"
tput cup 12 10
echo "5.Exit"
tput cup 20 5
echo "enter a choice[1-5] : "
read choice
case $choice in
1)echo "Today date is : 'date'";;
2)who;;
3)sh foen.sh;; #./fopen.sh
4)sh del.sh;; #./del.sh
5)echo "Thank You"
exit;; #to terminate the program
*)echo "choice wrong. try again";;
esac
--------------------------------------------------------------------------------------------------------------------------
6).Example of while loop.
ans="y"
while [$ans = "y" ]
do
echo "Enter a filename to open:"
read fn
if [ -e $fn -a -f $fn ]
then
cat $fn
else
echo "no such file"
fi
echo "Do u want to open one more file [y/n] : "
read ans
done
-------------------------------------------------------------------------------------------------------------------------
7).Example of while loop.
while true # until false
do
echo "Enter a filename to open : "
read fn
if [ -e $fn -a -f $fn ]
then
cat $fn
break
else
continue
fi
done
--------------------------------------------------------------------------------------------------------------------------
8).Example of sleep
#sleep is used for to stop the execution specified no of seconds.
while true
do
clear
tput cup 5 8
echo "WELCOME TO"
sleep 2
clear
echo "TECNOSOFT"
sleep 2
done
--------------------------------------------------------------------------------------------------------------------------
9).#Write a script create " n" no of users
echo -n "Enter no of users to create : "
read n
$i=l
while [ $i -le $n ]
do
$x=tecno$i #it creates users with tecno name
useradd $x
i='expr $i+1'
done
--------------------------------------------------------------------------------------------------------------------------
10).#Example of for loop
for i in 1 2 3 4 5
do
echo $i
done
--------------------------------------------------------------------------------------------------------------------------
11).#Example of for loop
a=10
b=20
c=30
for i in a b c
do
echo $i
done
--------------------------------------------------------------------------------------------------------------------------
12).#Example of for loop
a=10
b=20
c=30
for i in $a $b $c
do
echo $i
done
--------------------------------------------------------------------------------------------------------------------------
13).#Write a script to display all sub directories of current directory
for i in *
do
if [ -d $i ]
then
echo $i
fi
done
--------------------------------------------------------------------------------------------------------------------------
14).#Write a script to display all empty files in the current directory
for i in *
do
if [ ! -s$i ]
then echo $i #rm $i =>to delete empty file
fi
done
--------------------------------------------------------------------------------------------------------------------------
15).#Write a script to display all exe files in the current directory
for i in*
do
if [ -f $i -a -r $i -a -x $i ]
then
echo $i
done
--------------------------------------------------------------------------------------------------------------------------
16).#Write a script to display details of employees who are receiving salary more than 5000 from emp file
#101,hari,9000,10
#102,madhu,4000,20
#103,anu,000,30
#104,priya,8000,10
for i in 'cat emp'
do
j='echo $i | cut -d"," -f -3'
then
echo $i
fi
done
--------------------------------------------------------------------------------------------------------------------------
17).#Write a script retrieve details of employees who are receiving salary more than 5000 in deptno 10 from emp file and insert into emp1 file
#101,hari,9000,10
#102,madhu,4000,20
#103,anu,666666000,30
#104,priya,8000,10
for i in 'cat emp'
do
j='echo $i | cut -d"," -f 3'
k='echo $i | cut -d ","-f 4'
if [ $j -ge 5000 -a $k -eq 10 ]
then
echo $i>>emp1
fi
done
No comments:
Post a Comment