Unix Shell Script Program List 1 to 10
SCRIPT-1 Write a shell script which reads your name and department and print a
message.
echo "What is your name?"
read nm
echo "In which department are you?"
read dept
echo "My name is $nm."
echo "I'm a $dept Student."
OUTPUT:
$ ./a.sh
What is your name?
admin
In which department are you?
BCA
My name is admin.
I'm a BCA Student.
SCRIPT - 2 Write a shell script to check whether numbers are same or not.
echo "Enter first number"
read a
echo "Enter second number"
read b
if [ $a == $b ]
then
echo "a is equal to b"
else
echo "a is not equal to b"
fi
OUTPUT
$ ./n1.sh
Enter first number
30
Enter second number
20
a is not equal to b
SCRIPT-3 Write a shell script to check number using if..else.
echo "Enter number"
read count
if [ $count -eq 100 ]
then
echo "Count is 100"
elif [ $count -gt 100 ]
then
echo "Count is greater than 100"
else
echo "Count is less than 100"
fi
OUTPUT
$ ./n1.sh
Enter number
33
Count is less than 100
SCRIPT-4 Write a shell script on use of arithmetic operators.
echo "Enter First number"
read a
echo "Enter second number"
read b
val=`expr $a + $b`
echo "a + b : $val"
val=`expr $a - $b`
echo "a - b : $val"
val=`expr $a \* $b`
echo "a * b : $val"
val=`expr $b / $a`
echo "b / a : $val"
val=`expr $b % $a`
echo "b % a : $val"
if [ $a == $b ]
then
echo "a is equal to b"
fi
if [ $a != $b ]
then
echo "a is not equal to b"
fi
OUTPUT
$ ./a.sh
Enter First number
30
Enter second number
20
a + b : 50
a - b : 10
a * b : 600
b / a : 0
b % a : 20
a is not equal to b
SCRIPT-5 Write a shell script to check withdraw amount.
echo "Balance = 1000"
echo "Withdraw amount:"
read a
if [ $a -gt 1000 ]
then
echo "Insufficient Balance!"
else
if [ $a -gt 950 ]
then
echo "Warning! Keep 50 in your account to keep it active!"
fi
fi
OUTPUT
$ ./n1.sh
Balance = 1000
Withdraw amount:
970
Warning! Keep 50 in your account to keep it active!
SCRIPT-6 Write a Shell Script to Generate Fibonacci Series
echo "Enter a Number :"
read num
a=0
b=1
echo "The Fibonacci sequence: "
for (( i=0; i<=num; i++ ))
do
echo -n "$a "
f =`expr $a + $b`
a=$b
b=$f
done
OUTPUT
$ ./n1.sh
Enter a Number:
5
The Fibonacci sequence:
0 1 1 2 3 5
SCRIPT-7 Write a Shell script to find whether number is even or odd.
echo "Enter number:"
read num
if [ `expr $num % 2` -eq 0 ]
then
echo "$num is even"
else
echo "$num is odd"
fi
OUTPUT
$ ./n1.sh
Enter number:
65
65 is odd
SCRIPT-8 Menu driven Shell script that Lists current directory, Prints Working
Directory, displays Date and displays Calendar.
chk=y
echo -e "\nMENU:
\n1. List Current Directory
\n2. Date
\n3. Print Working Directory
\n4. calender"
while [ "$chk" = "y" ]
do
echo -e "\nEnter your choice:"
read choice
case "$choice" in
1) ls ;;
2) date ;;
3) pwd ;;
4) cal ;;
*) echo "Invalid Choice"
esac
echo -e "\nDo you want to continue? Enter y to continue"
read chk
done
OUTPUT
$ ./n1.sh
MENU:
1. List Current Directory
2. Date
3. Print Working Directory
4. calender
Enter your choice:
3
/cygdrive/e
Do you want to continue? Enter y to continue
y
Enter your choice:
5
Invalid Choice
Do you want to continue? Enter y to continue
y
Enter your choice:
4
February 2018
Su Mo Tu We Th Fr Sa
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28
SCRIPT -9 Write a Shell Script to find the factorial value of any number.
echo -e "Enter a number: \c"
read num
fact=1
n=$num
while [ $num -ge 1 ]
do
fact=`expr $fact \* $num`
num=`expr $num - 1`
done
echo -e "Factorial of $n is $fact"
OUTPUT
$ ./n1.sh
Enter a number: 5
Factorial of 5 is 120
SCRIPT-10 Write a Shell Script to check character using case..easc.
echo "Enter any character"
read d
case $d in
[a-z])echo "It is a Lower case alphabet"
;;
[A-Z])echo "It is a Upper case alphabet"
;;
[0-9])echo "It is a digit"
;;
+|-|*|/) echo "Arithmetic Operator"
;;
*)echo "It is a Special Symbol"
esac
OUTPUT
$ ./n1.sh
Enter any character
+
Arithmetic Operator
message.
echo "What is your name?"
read nm
echo "In which department are you?"
read dept
echo "My name is $nm."
echo "I'm a $dept Student."
OUTPUT:
$ ./a.sh
What is your name?
admin
In which department are you?
BCA
My name is admin.
I'm a BCA Student.
SCRIPT - 2 Write a shell script to check whether numbers are same or not.
echo "Enter first number"
read a
echo "Enter second number"
read b
if [ $a == $b ]
then
echo "a is equal to b"
else
echo "a is not equal to b"
fi
OUTPUT
$ ./n1.sh
Enter first number
30
Enter second number
20
a is not equal to b
SCRIPT-3 Write a shell script to check number using if..else.
echo "Enter number"
read count
if [ $count -eq 100 ]
then
echo "Count is 100"
elif [ $count -gt 100 ]
then
echo "Count is greater than 100"
else
echo "Count is less than 100"
fi
OUTPUT
$ ./n1.sh
Enter number
33
Count is less than 100
SCRIPT-4 Write a shell script on use of arithmetic operators.
echo "Enter First number"
read a
echo "Enter second number"
read b
val=`expr $a + $b`
echo "a + b : $val"
val=`expr $a - $b`
echo "a - b : $val"
val=`expr $a \* $b`
echo "a * b : $val"
val=`expr $b / $a`
echo "b / a : $val"
val=`expr $b % $a`
echo "b % a : $val"
if [ $a == $b ]
then
echo "a is equal to b"
fi
if [ $a != $b ]
then
echo "a is not equal to b"
fi
OUTPUT
$ ./a.sh
Enter First number
30
Enter second number
20
a + b : 50
a - b : 10
a * b : 600
b / a : 0
b % a : 20
a is not equal to b
SCRIPT-5 Write a shell script to check withdraw amount.
echo "Balance = 1000"
echo "Withdraw amount:"
read a
if [ $a -gt 1000 ]
then
echo "Insufficient Balance!"
else
if [ $a -gt 950 ]
then
echo "Warning! Keep 50 in your account to keep it active!"
fi
fi
OUTPUT
$ ./n1.sh
Balance = 1000
Withdraw amount:
970
Warning! Keep 50 in your account to keep it active!
SCRIPT-6 Write a Shell Script to Generate Fibonacci Series
echo "Enter a Number :"
read num
a=0
b=1
echo "The Fibonacci sequence: "
for (( i=0; i<=num; i++ ))
do
echo -n "$a "
f =`expr $a + $b`
a=$b
b=$f
done
OUTPUT
$ ./n1.sh
Enter a Number:
5
The Fibonacci sequence:
0 1 1 2 3 5
SCRIPT-7 Write a Shell script to find whether number is even or odd.
echo "Enter number:"
read num
if [ `expr $num % 2` -eq 0 ]
then
echo "$num is even"
else
echo "$num is odd"
fi
OUTPUT
$ ./n1.sh
Enter number:
65
65 is odd
SCRIPT-8 Menu driven Shell script that Lists current directory, Prints Working
Directory, displays Date and displays Calendar.
chk=y
echo -e "\nMENU:
\n1. List Current Directory
\n2. Date
\n3. Print Working Directory
\n4. calender"
while [ "$chk" = "y" ]
do
echo -e "\nEnter your choice:"
read choice
case "$choice" in
1) ls ;;
2) date ;;
3) pwd ;;
4) cal ;;
*) echo "Invalid Choice"
esac
echo -e "\nDo you want to continue? Enter y to continue"
read chk
done
OUTPUT
$ ./n1.sh
MENU:
1. List Current Directory
2. Date
3. Print Working Directory
4. calender
Enter your choice:
3
/cygdrive/e
Do you want to continue? Enter y to continue
y
Enter your choice:
5
Invalid Choice
Do you want to continue? Enter y to continue
y
Enter your choice:
4
February 2018
Su Mo Tu We Th Fr Sa
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28
SCRIPT -9 Write a Shell Script to find the factorial value of any number.
echo -e "Enter a number: \c"
read num
fact=1
n=$num
while [ $num -ge 1 ]
do
fact=`expr $fact \* $num`
num=`expr $num - 1`
done
echo -e "Factorial of $n is $fact"
OUTPUT
$ ./n1.sh
Enter a number: 5
Factorial of 5 is 120
SCRIPT-10 Write a Shell Script to check character using case..easc.
echo "Enter any character"
read d
case $d in
[a-z])echo "It is a Lower case alphabet"
;;
[A-Z])echo "It is a Upper case alphabet"
;;
[0-9])echo "It is a digit"
;;
+|-|*|/) echo "Arithmetic Operator"
;;
*)echo "It is a Special Symbol"
esac
OUTPUT
$ ./n1.sh
Enter any character
+
Arithmetic Operator
Comments
Post a Comment