Shell Script Programs using Conditional controls
To create Shell Script Programs using Conditional controls
#!/bin/bash
#Student Marksheet
echo -e "\t\t******student mark sheet******"
echo "Enter the mark 1 :"
read m1
echo "Enter the mark 2 :"
read m2
echo "Enter the mark 3 :"
read m3
echo "Enter the mark 4 :"
read m4
echo "Enter the mark 5 :"
read m5
tot=$(( $m1 + $m2 + $m3 + $m4 + $m5 ))
avg=$(( tot / 5 ))
echo -e "\t Total :
$tot "
echo -e "\t Average : $avg "
if [ $m1 -ge 50 -a $m2
-ge 50 -a $m3 -ge 50 -a $m4 -ge 50 -a
$m5 -ge 50 ]
then
echo -e "\t Result
: PASS "
if [ $avg -ge 50 ]
then
if [ $avg -ge 90 ]
then
echo -e "\t Grade : S
"
elif [ $avg -ge 80 ]
then
echo -e
"\t Grade : A "
elif [ $avg -ge
70 ]
then
echo -e
"\t Grade : B "
elif [ $avg -ge
60 ]
then
echo -e
"\t Grade : C "
else
echo -e
"\t Grade : D "
fi
else
echo -e "\t
Grade : E "
fi
else
echo -e "\t Result
: FAIL "
echo -e "\t Grade
: not applicable "
fi
Output :
[root@localhost runaable_sh]# bash marksheet.sh
******student
mark sheet******
Enter the mark 1 :
98
Enter the mark 2 :
69
Enter the mark 3 :
98
Enter the mark 4 :
99
Enter the mark 5 :
97
Total
: 461
Average : 92
Result
: PASS
Grade
: S
[root@localhost runaable_sh]#
Arithmetic operations
#!/bin/bash
# Arithmetic operations
echo -e "\t\t*******Arithmetic Operations******* "
op=1
while [ $op -eq 1 ]
do
echo -e "\t 1).Addition \n\t 2).Subtraction \n\t
3).Multiplication \n\t 4).Division \n\t 5).Modulodivision"
echo -e "\tEnter Your Choice : "
read ch
echo -e "\tEnter the A value : "
read a
echo -e "\tEnter the B value : "
read b
case $ch in
1)echo -e " Addition
"
expr $a + $b
;;
2)echo -e
" Subtraction "
expr $a - $b
;;
3)echo -ne " \t
Multiplication : "
expr $a \* $b
;;
4)echo -ne " \t
Division : "
expr $a / $b
;;
5)echo -ne " \t
Modulodivision : "
expr $a % $b
;;
esac
echo "Do U want to Continue : (press 1 / exit 0)"
read op
done
OUTPUT :
[root@localhost runaable_sh]# bash arith_operation.sh
*******Arithmetic
Operations*******
1).Addition
2).Subtraction
3).Multiplication
4).Division
5).Modulodivision
Enter Your Choice :
1
Enter the A value :
53
Enter the B value :
9
Addition
62
Do U want to Continue : (press 1 / exit 0)
1
1).Addition
2).Subtraction
3).Multiplication
4).Division
5).Modulodivision
Enter Your Choice :
3
Enter the A value :
8
Enter the B value :
6
Multiplication : 48
Do U want to Continue : (press 1 / exit 0)
1
1).Addition
2).Subtraction
3).Multiplication
4).Division
5).Modulodivision
Enter Your Choice :
5
Enter the A value :
7
Enter the B value :
2
Modulodivision : 1
Do U want to Continue : (press 1 / exit 0)
Greatest Among three numbers
#!/bin/bash
# Greatest Among three numbers
echo " Enter A value: "
read a
echo " Enter B value: "
read b
echo " Enter C value: "
read c
if [ $a -gt $b ] && [ $a -gt $c ]
then
echo "A value is greater"
elif [ $b -gt $a ] && [ $b -gt $c ]
then
echo "B value is greatest "
else
echo "C value is greatest "
fi
OUTPUT :
[root@localhost runaable_sh]# bash gr3.sh
Enter A value:
10
Enter B value:
6
Enter C value:
8
A value is greater
string concatenation
[root@localhost runaable_sh]#
#!/bin/bash
# string concatenation
echo Enter first string:
read s1
echo Enter second string:
read s2
s3=$s1$s2
len=`echo $s3 | wc -c`
len=`expr $len - 1`
echo Concatinated stringis $s3 of length $len
Output :
[root@localhost runaable_sh]# bash strcat.sh
Enter first string:
balachandar
Enter second string:
sri
Concatinated stringis balachandarsri of length 14
Script Programs using Conditional controls
AIM :
To create Shell Script Programs using Conditional controls.
Sum of digit
#!/bin/bash
# read the number and find the Sum of digit
echo "enter the no : "
read input
sum=0
while [ $input -gt 0 ]
do
rem=$(( input % 10 ))
sum=$(( sum + rem ))
input=$(( input / 10 ))
done
echo -e " Sum of digit :
$sum "
Output :
[root@localhost runaable_sh]# bash sumofdigit.sh
enter the no :
4568
Sum of digit : 23
Reverse the given number
#!/bin/bash
# reverse the given number
echo "Enter the number
"
read input
while [ $input -gt 0 ]
do
rem=$(( input % 10 ))
sum=$((( sum * 10 ) + rem ))
input=$(( input / 10 ))
done
echo -e " Sum of digit :
$sum "
OUTPUT :
[root@localhost runaable_sh]# bash reverse_no.sh
Enter the number
963
Sum of digit : 369
Check the given number is palindrome or not
#!/bin/bash
# Check the given number is palindrome or not
echo "Enter the number
"
read input
temp=$input
while [ $input -gt 0 ]
do
rem=$(( input % 10 ))
sum=$((( sum * 10 ) + rem ))
input=$(( input / 10 ))
done
if [ $temp -eq $sum ]
then
echo -e " $temp is a palindrome number !"
else
echo -e " $temp is not a palindrome number! "
fi
output :
[root@localhost runaable_sh]# bash palindrome_no.sh
Enter the number
121
121 is a palindrome
number !
[root@localhost runaable_sh]# bash palindrome_no.sh
Enter the number
4569
4569 is not a palindrome
number!
Prime Number Checking
#!/bin/sh
#Prime Number Checking
i=2
rem=1
echo -e "Enter a number: \c"
read num
if [ $num -lt 2 ]; then
echo -e "$num is not
prime\n"
exit 0
fi
while [ $i -le `expr $num / 2` -a $rem -ne 0 ]; do
rem=`expr $num % $i`
i=`expr $i + 1`
done
if [ $rem -ne 0 ]; then
echo -e "$num is
prime\n"
else
echo -e "$num is not
prime\n"
fi
OUTPUT :
[root@localhost runaable_sh]# bash prime.sh
Enter a number: 7
7 is prime
[root@localhost runaable_sh]# bash prime.sh
Enter a number: 9
9 is not prime
Check The Given String Is palindrome or not
#!/bin/bash
#strpoly.sh
clear
echo "Enter a string to be entered:"
read str
echo
len=`echo $str | wc -c`
len=`expr $len - 1`
i=1
j=`expr $len / 2`
while test $i -le $j
do
k=`echo $str | cut -c $i`
l=`echo $str | cut -c $len`
if test $k != $l
then
echo "String is not palindrome"
exit
fi
i=`expr $i + 1`
len=`expr $len - 1`
done
echo "String is palindrome"
Output :
[root@localhost runaable_sh]# bash strpoly.sh
Enter a string to be entered:
nalan
String is palindrome
[root@localhost runaable_sh]# bash strpoly.sh
Enter a string to be entered:
balachandar
String is not palindrome
[root@localhost runaable_sh]#
fibonacci series
#!/bin/bash
#fibonacci series
echo "Enter How many numbers:"
read num
num1=0
num2=1
echo "Fibonacci series:"
echo $num1
echo $num2
count=2
while [ $count -le $num ]
do
num3=`expr $num1 + $num2`
echo $num3
num1=$num2
num2=$num3
count=`expr $count + 1`
done
Output :
[root@localhost runaable_sh]# bash fibo2.sh
Enter How many numbers:
10
Fibonacci series:
0
1
1
2
3
5
8
13
21
34
55
[root@localhost runaable_sh]
Sum of cubes
#!/bin/bash
# sum of cubes
n=1
while [ $n -le 999 ]
do
sum=0
p=$n
while [ $p -ne 0 ]
do
tmp=`expr $p \% 10`
p=`expr $p \/ 10`
q=`expr $tmp \* $tmp \* $tmp`
sum=`expr $sum + $q`
done
if [ $sum = $n ]
then
echo $n
fi
n=`expr $n + 1`
done
Output :
[root@localhost runaable_sh]# bash sumcube.sh
1
153
370
371
407
[root@localhost runaable_sh]#
Armstrong Number
#!/bin/bash
#amstrong.sh
echo "Enter the number"
read n
t=$n
s=0
b=0
c=10
while [ $n -gt $b ]
do
r=`expr $n % $c`
i=`expr $r \* $r \* $r`
s=`expr $s + $i`
n=`expr $n / $c`
done
echo $s
if [ $s -eq $t ]
then
echo "Amstrong number"
else
echo "Not an Armstrong number"
fi
Output :
[root@localhost runaable_sh]# bash amstrong.sh
Enter the number
153
153
Amstrong number
[root@localhost runaable_sh]# bash amstrong.sh
Enter the number
658
853
Not an Armstrong number
[root@localhost runaable_sh]#
Shell Script Programs using Functions
AIM :
To create Shell Script Programs using Functions
FECTORIAL USING RECURSIVE FUNCTION
#!/bin/bash
# recurfact.sh - Shell script to to find factorial of given
command line arg
factorial(){
local i=$1
local f
declare -i i
declare -i f
# factorial() is called
until the value of $f is returned and is it is <= 2
# This is called the
recursion
[ $i -le 2 ] &&
echo $i || { f=$(( i - 1)); f=$(factorial $f); f=$(( f * i )); echo $f; }
}
# display usage
[ $# -eq 0 ] && { echo "Usage: $0 number";
exit 1; }
# call factorial
factorial $1
OUTPUT :
[root@localhost runaable_sh]# bash recurfact.sh 5
120
FATORIAL
#!/bin/bash
# fact.sh - Shell script to to find factorial of given command
line arg
ounter=$1 #first argument
factorial=1
while [ $counter -gt 0 ] #while counter > 0
do
factorial=$((
$factorial * $counter ))
counter=$(( $counter -
1 ))
done
echo $factorial
output :
[root@localhost runaable_sh]# bash recurfact.sh 5
12
convert decimal to binary and vice-versa
#!/bin/bash
#Shell Script to convert decimal to binary and vice-versa
tput clear
echo "Conversion of decimal to Binary and Binary to
Decimal"
echo "1. Convert Decimal to Binary"
echo "2. Convert Binary to Decimal"
echo "3. Exit"
echo "Enter ur choice:"
read ch
case $ch in
1) echo "Enter any decimal no:"
read num
rem=1
bno=" "
while [ $num -gt 0 ]
do
rem=`expr $num % 2 `
bno=$bno$rem
num=`expr $num / 2 `
done
i=${#bno}
final=" "
while [ $i -gt 0 ]
do
rev=`echo $bno | awk '{ printf substr( $0,'$i',1 ) }'`
final=$final$rev
i=$(( $i - 1 ))
done
echo "Equivalent Binary no:" $final ;;
2) echo "Enter any Binary no;"
read bino
len=${#bino}
i=1
pow=$((len - 1 ))
while [ $i -le $len ]
do
n=`echo $bino | awk '{ printf substr( $0,'$i',1 )}' `
j=1
p=1
while [ $j -le $pow ]
do
p=$(( p * 2 ))
j=$(( j + 1 ))
done
dec=$(( n * p ))
findec=$(( findec + dec ))
pow=$((pow - 1 ))
i=$(( i + 1 ))
done
echo "Equivalent Decimal no:"$findec ;;
3) echo "Enter correctly:" ;;
esac
Output :
[root@localhost Desktop]# bash btoc.sh
Conversion of decimal to Binary and Binary to Decimal
1. Convert Decimal to Binary
2. Convert Binary to Decimal
3. Exit
Enter ur choice:
1
Enter any decimal no:
10
Equivalent Binary no: 1010
[root@localhost Desktop]# bash btoc.sh
Conversion of decimal to Binary and Binary to Decimal
1. Convert Decimal to Binary
2. Convert Binary to Decimal
3. Exit
Enter ur choice:
2
Enter any Binary no;
1010
Equivalent Decimal no:10
#alternate option (Decimal to Binary)
ip1=10
[root@localhost runaable_sh]# echo "obase=2;$ip1" | bc
1010
#alternate option ( Binary to Decimal)
[root@localhost runaable_sh]# echo "ibase=2;1010" | bc
10