Code:
echo Enter month
month=(30 31 29)
read mon
case "$mon" in
[jJ][aA][nN]*) echo "No.of days : ${month[1]} ";;
[fF][eE][bB]*) echo enter year
read year
let rem="$year % 4"
if [ $rem -eq 0 ] ; then
echo "No. of days : ${month[2]}"
else
echo "No. of days : 28 "
fi;;
[mM][aA][rR]*) echo "No. of days : ${month[1]}";;
[aA][pP][rR]*) echo "No. of days: ${month[0]}";;
[mM][aA][yY]*) echo "No. of days: ${month[1]}";;
[jJ[uU][nN]*) echo "No. of days: ${month[0]}";;
[jJ][uU][lL]*) echo "No. of days: ${month[1]}";;
[aA][uU][gG]*) echo " No. of days: ${month[1]}";;
[sS][eE][pP]*) echo " No. of days: ${month[0]}";;
[aA][cC][tT]*) echo "No. of days: ${month[1]}";;
[nN][oO][vV]*) echo "No. of days: ${month[0]}";;
[dD][eE][cC]*) echo "No. of days: ${month[1]}";;
esac
Output:
[ty2011330@localhost ~]$ vi month.sh
[ty2011330@localhost ~]$ sh month.sh
Enter month
feb
enter year
2005
No. of days : 28
Tuesday, 11 June 2013
shell script to accept two numbers from user and display n^y using while loop.
Code:
echo Enter value for n:
read n
echo Enter value for y:
read y
iter=1
sum=1
while [ $iter -le $y ]
do
let sum=sum*n
let iter=$iter+1
done
echo $sum
Output:
[ty2011330@localhost ~]$ sh file21.sh
Enter value for n:
2
Enter value for y:
3
8
echo Enter value for n:
read n
echo Enter value for y:
read y
iter=1
sum=1
while [ $iter -le $y ]
do
let sum=sum*n
let iter=$iter+1
done
echo $sum
Output:
[ty2011330@localhost ~]$ sh file21.sh
Enter value for n:
2
Enter value for y:
3
8
shell script to accept a number from user and display multiplication table of given number.
Code:
echo "Multiplication Table:"
tab=1
i=1
a=10
echo "Input Number :"
read x
echo
echo "Table of $x"
for i in 1 2 3 4 5 6 7 8 9 10
do
i=`expr $x \* $i`
echo $i
done
Output:
[pritam1@localhost ~]$ sh demofor.sh
Multiplication Table:
Input Number :
15
Table of 15
15
30
45
60
75
90
105
120
135
150
echo "Multiplication Table:"
tab=1
i=1
a=10
echo "Input Number :"
read x
echo
echo "Table of $x"
for i in 1 2 3 4 5 6 7 8 9 10
do
i=`expr $x \* $i`
echo $i
done
Output:
[pritam1@localhost ~]$ sh demofor.sh
Multiplication Table:
Input Number :
15
Table of 15
15
30
45
60
75
90
105
120
135
150
shell script for menu driven program to see the contents of /etc/passwd, to see the list of users, to see present working directory or to exit.
Code:
echo enter
echo 1 to see the contents of /etc/passwd
echo 2 to see list of users
echo 3 to see present working directory
echo 4 exit
echo enter your choice
read n
case $n in
1) ls /etc ;;
2) ls /home;;
3) pwd;;
4) exit;;
*) echo enter the choice as 1,2,3or4;;
esac
Output:
[ty2011330@localhost ~]$ sh file13.sh
enter
1 to see the contents of /etc/passwd
2 to see list of users
3 to see present working directory
4 exit
enter your choice
2
administrator pritam ty11305 ty11313 ty11321 ty11329 ty11337 ty11345
amita pritam1 ty11306 ty11314 ty11322 ty11330 ty11338 ty11346
dipti shital ty11307 ty11315 ty11323 ty11331 ty11339 ty11347
exam5154 shital1 ty11308 ty11316 ty11324 ty11332 ty11340 ty11348
enter your choice
1
a2ps.cfg inittab quotatab
a2ps-site.cfg inputrc racoon
acpi iproute2 raddb
enter your choice
3
/home/tybsc
enter your choice
4
2.
ans=Y
while [ $ans = "Y" ]
do
echo " MENU"
echo "1 . Query on product"
echo "2 . Query on customer"
echo "0 . Exit"
echo "enter ur choice : "
read ch
case "$ch" in
1) echo "Enter product number :"
read pno
grep $pno masterdata
echo "Query on other product : "
read ans
if(test $ans = "Y")
then
continue
fi;;
2) echo "Enter customer number :"
read cno
grep $cno transdata;;
0) exit;;
*) echo "Invalid choice. Try again..."
esac
done
echo enter
echo 1 to see the contents of /etc/passwd
echo 2 to see list of users
echo 3 to see present working directory
echo 4 exit
echo enter your choice
read n
case $n in
1) ls /etc ;;
2) ls /home;;
3) pwd;;
4) exit;;
*) echo enter the choice as 1,2,3or4;;
esac
Output:
[ty2011330@localhost ~]$ sh file13.sh
enter
1 to see the contents of /etc/passwd
2 to see list of users
3 to see present working directory
4 exit
enter your choice
2
administrator pritam ty11305 ty11313 ty11321 ty11329 ty11337 ty11345
amita pritam1 ty11306 ty11314 ty11322 ty11330 ty11338 ty11346
dipti shital ty11307 ty11315 ty11323 ty11331 ty11339 ty11347
exam5154 shital1 ty11308 ty11316 ty11324 ty11332 ty11340 ty11348
enter your choice
1
a2ps.cfg inittab quotatab
a2ps-site.cfg inputrc racoon
acpi iproute2 raddb
enter your choice
3
/home/tybsc
enter your choice
4
2.
ans=Y
while [ $ans = "Y" ]
do
echo " MENU"
echo "1 . Query on product"
echo "2 . Query on customer"
echo "0 . Exit"
echo "enter ur choice : "
read ch
case "$ch" in
1) echo "Enter product number :"
read pno
grep $pno masterdata
echo "Query on other product : "
read ans
if(test $ans = "Y")
then
continue
fi;;
2) echo "Enter customer number :"
read cno
grep $cno transdata;;
0) exit;;
*) echo "Invalid choice. Try again..."
esac
done
Shell Program to find the type of the character entered
echo Enter any character
read c
case $c in
[a-z]) echo Small Case letter;;
[A-Z]) echo Capital letter;;
[0-9]) echo digit;;
?) echo special symbol;;
*) echo more than one character, reenter;;
esac
11)#Program to find the pattern
echo Enter the word
read str
case $str in
[aeiou]*) echo The word begins with a vowel;;
[0-9]*) echo The word starts with a digit;;
*[0-9]) echo The word ends with a digit;;
????) echo The word entered is 4 lettered word;;
*) echo The word entered is either starts with a
consonant or incorrect input;;
esac
read c
case $c in
[a-z]) echo Small Case letter;;
[A-Z]) echo Capital letter;;
[0-9]) echo digit;;
?) echo special symbol;;
*) echo more than one character, reenter;;
esac
11)#Program to find the pattern
echo Enter the word
read str
case $str in
[aeiou]*) echo The word begins with a vowel;;
[0-9]*) echo The word starts with a digit;;
*[0-9]) echo The word ends with a digit;;
????) echo The word entered is 4 lettered word;;
*) echo The word entered is either starts with a
consonant or incorrect input;;
esac
Shell Script to accept a file name and check whether the file name exist or not in current directory .
Code:
echo -n "Enter the Filename : "
read fname
if [ -f $fname ]
then
echo "File exists in the current directory. "
else
echo "File with this name does not exist in the current directory."
fi
Output:
[ty2011330@localhost ~]$ sh j03.sh
Enter the Filename : file5
File exists in the current directory.
[ty2011330@localhost ~]$ sh j03.sh
Enter the Filename : file12
File with this name doesnot exist in the current directory.
echo -n "Enter the Filename : "
read fname
if [ -f $fname ]
then
echo "File exists in the current directory. "
else
echo "File with this name does not exist in the current directory."
fi
Output:
[ty2011330@localhost ~]$ sh j03.sh
Enter the Filename : file5
File exists in the current directory.
[ty2011330@localhost ~]$ sh j03.sh
Enter the Filename : file12
File with this name doesnot exist in the current directory.
Shell Script to accept a number from user and check whether the number is even or odd .
Code:
echo -n "Enter the number : "
read n
let rem=n%2
if test $rem -eq 0
then
echo "$n is a even number ."
else
echo "$n is a odd number ."
fi
Output:
[ty2011330@localhost ~]$ sh j11.sh
Enter the number : 7
7 is a odd number .
[ty2011330@localhost ~]$ sh j11.sh
Enter the number : 10
10 is a even number .
echo -n "Enter the number : "
read n
let rem=n%2
if test $rem -eq 0
then
echo "$n is a even number ."
else
echo "$n is a odd number ."
fi
Output:
[ty2011330@localhost ~]$ sh j11.sh
Enter the number : 7
7 is a odd number .
[ty2011330@localhost ~]$ sh j11.sh
Enter the number : 10
10 is a even number .
Subscribe to:
Posts (Atom)