0% found this document useful (0 votes)
150 views14 pages

Shell Programs

The document contains a collection of shell scripts that perform various programming tasks, including argument display, array manipulation, binary search, sorting algorithms, and mathematical calculations such as factorial and prime number generation. It also includes functionality for file operations, user input handling, and basic calculator operations. Each section is structured with prompts for user input and outputs the results of the computations or operations performed.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
150 views14 pages

Shell Programs

The document contains a collection of shell scripts that perform various programming tasks, including argument display, array manipulation, binary search, sorting algorithms, and mathematical calculations such as factorial and prime number generation. It also includes functionality for file operations, user input handling, and basic calculator operations. Each section is structured with prompts for user input and outputs the results of the computations or operations performed.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Argument display

if [ $# -eq 1 ]
then
echo "One"
elif [ $# -eq 2 ]
then
echo "Two"
else
echo "Error"
fi

Array

echo "Enter the elements:"


for ((i=0;i<=4;i++))
do
read a[$i]
done
echo "Elements of the array:"
for ((i=0;i<=4;i++))
do
echo ${a[$i]}
done

Binary Search

echo -n "Enter the number of the elements: "


read n

echo "Enter elements: "


for((i=1;i<=n;i++))
do
read a[$i]
done

echo -e "\nEnter item to be searched: "


read item

beg=1
end=$n
mid=`expr $beg + $end / 2`
while [ ${a[$mid]} -ne $item -a $beg -le $end ]
do
if [ $item -lt ${a[$mid]} ]
then end=`expr $mid - 1`
else beg=`expr $mid + 1`
fi
mid=`expr $beg + $end / 2`
done
if [ ${a[$mid]} -eq $item ]
then echo "Item found at $mid location."
else echo "Item not found"
fi
Bubble Sort

echo -n "Enter the number of nos:"


read n
echo "Enter the list of numbers:"
for((i=0;i<n;i++))
do
read a[$i]
done

for((k=0;k<n-1;k++))
do
ptr=0
j=`expr $n - $k`
while [ $ptr -le $j ]
do
ptr1=`expr $ptr + 1`
if [ ${a[$ptr]} -gt ${a[$ptr1]} ]
then
t=${a[$ptr]}
a[ptr]=${a[$ptr1]}
a[ptr1]=$t
fi
ptr=`expr $ptr + 1`
done
done

echo "The sorted list is:"


for((i=0;i<n;i++))
do
echo -n " "${a[$i]}
done

Delete Computer

echo -n "Enter a file name"


read a
if [ -e $a ]
then
grep -v computer $a>[Link]
mv [Link] $a
else
echo "The file $a does not exist"
fi

Factorial
echo -n "Enter a number: "
read n
i=$n
fact=1
while [ $i -ge 1 ]
do
​ fact=`expr $fact \* $i`
​ i=`expr $i - 1`
done
echo "Factorial= " $fact

Fibonacci

# Fibonacci Series
echo -n "Enter the length of the series:"
read l
a=0
b=1
echo $a
echo $b
i=3
while [ $i -le $l ]
do
c=`expr $a + $b`
echo $c
a=$b
b=$c
i=`expr $i + 1`
done

Find maximum minimum

echo -n "Enter the number of nos:"


read n
echo "Enter the list of numbers:"

for((i=0;i<n;i++))
do
read a[$i]
done
max=${a[0]}
min=${a[0]}
for((i=1;i<n;i++))
do
if [ ${a[$i]} -lt $min ]
then
min=${a[$i]}
fi
if [ ${a[$i]} -gt $max ]
then
max=${a[$i]}
fi
done
echo "The minimun number is:$min"
echo "The maximum number is:$max"
Gcd Lcm

echo -n "Enter two numbers: "


read a b
if [ $a -ge $b ]
then
​ m=$a
​ n=$b
else
​ m=$b
​ n=$a
fi
r=1
while [ $r -ne 0 ]
do
​ r=`expr $m % $n`
​ m=$n
​ n=$r
done
echo "GCD= " $m
l=`expr $a \* $b / $m`
echo "LCM= " $l

Grade

echo -n "Enter marks of four subjects: "


read w x y z
tot=`echo $w+$x+$y+$z|bc`
echo $tot
cr=`echo $tot/400*10|bc -l`
if [ "$(echo $cr '>=' 8|bc -l)" -eq 1 ]
then
​ echo grade:O
elif [ "$(echo $cr'>='7.0|bc -l)" -eq 1 -a "$(echo $cr'<='7.9|bc -l)" -eq
1 ]
then
​ echo grade:A
elif [ "$(echo $cr'>='6.0|bc -l)" -eq 1 -a "$(echo $cr'<='6.9|bc -l)" -eq
1 ]
then
echo grade:B
elif [ "$(echo $cr'>='5.0|bc -l)" -eq 1 -a "$(echo $cr'<='5.9|bc -l)" -eq
1 ]
then
echo grade:C
elif [ "$(echo $cr'>='4.0|bc -l)" -eq 1 -a "$(echo $cr'<='4.9|bc -l)" -eq
1 ]
then
echo grade:D
elif [ "$(echo $cr'<'4.0|bc -l)" -eq 1 ]
then
​ echo Suppliment
fi
Insertion Sort

echo -n "Enter the number of nos:"


read n
echo "Enter the list of numbers:"
for((i=0;i<n;i++))
do
read a[$i]
done

for((i=0;i<n;i++))
do
for((k=i;k>0;k--))
do
p=`expr $k - 1`
if [ ${a[$k]} -lt ${a[$p]} ]
then
temp=${a[$k]}
a[$k]=${a[$p]}
a[$p]=$temp
fi
done
echo "After Pass:"
for((j=0;j<n;j++))
do
echo -n " ${a[$j]}"
done
echo ""
done

Leap Year

echo -n "Enter a year:"


read y
a=`expr $y % 100`
if [ $a -eq 0 ]
then
b=`expr $y % 400`
if [ $b -eq 0 ]
then
echo "It is a leap year"
else
echo "It is not a leap year"
fi
else
c=`expr $y % 4`
if [ $c -eq 0 ]
then
echo "It is a leap year"
fi
fi
Linear Search

echo -n "Enter the number of nos:"


read n
echo "Enter the list of numbers:"
for((i=0;i<n;i++))
do
read a[$i]
done

echo "Enter the number to be searched:"


read num

flag=0
for((i=0;i<n;i++))
do
if [ ${a[$i]} -eq $num ]
then
echo "The index of the data is $i"
flag=1
fi
done

if [ $flag -eq 0 ]
then
echo "Data not found"
fi

Lower To Upper

echo -n "Enter a string:"


read str
ustr=`echo $str|tr "[a-z]" "[A-Z]"`
echo $ustr

Odd or Even number

#Check a number whether it is odd or even

echo -n "Enter a number:" #READ A NUMBER FROM USER


read num

r=`expr $num % 2` #CALCULATE THE REMAINDER


if [ $r -eq 0 ] #CHECK THE REMAINDER IF ZERO OR NOT
then
echo $num "is even." #PRINT THE NUMBER AS EVEN IF REMAINDER IS
ZERO
else
echo $num "is odd." #PRINT THE NUMBER AS ODD IF REMAINDER IS
NOT ZERO
fi #END OF IF
Palindrome

echo -n "Enter a string:"


read str1
str2=""
ln=`expr length $str1`
while [ $ln -gt 0 ]
do
c=`expr substr $str1 $ln 1`
str2=$str2$c
ln=`expr $ln - 1`
done
if [ $str1 = $str2 ]
then
echo "Palindrome"
else
echo "Not Palindrome"
fi

Power

echo -n "Enter x and y: "


read x y
i=1
mul=1
while [ $i -le $y ]
do
​ mul=`expr $mul \* $x`
​ i=`expr $i + 1`
done
echo "Result= "$mul

Prime

#Prime number
echo -n "Enter the lower limit:"
read l
echo -n "Enter the upper limit:"
read u
n=$l
while [ $n -le $u ]
do
flag=0
d=`expr $n / 2`
i=2
while [ $i -le $d ]
do
r=`expr $n % $i`
if [ $r -eq 0 ]
then
flag=1
break
fi
i=`expr $i + 1`
done
if [ $flag -eq 0 ]
then
echo $n" "
fi
n=`expr $n + 1`
done

#BASIC CALCULATOR

echo "1. ADDITION"


echo "2. SUBSTRACTION"
echo "3. MULTIPLICATION"
echo "4. DIVISION"

#READ THE USER'S CHOICE

echo -n "Enter your choice:"


read ch

case $ch in

#ADDITION
1) echo -n "Enter the first number:" #READ THE NUMBERS FROM USER
read a
echo -n "Enter the second number:"
read b
c=`expr $a + $b`
echo "The result is "$c #PRINT THE RESULT
;;

#SUBSTRACTION
2) echo -n "Enter the first number:" #READ THE NUMBERS FROM USER
read a
echo -n "Enter the second number:"
read b
c=`expr $a - $b`
echo "The result is "$c #PRINT THE RESULT
;;

#MULTIPLICATION
3) echo -n "Enter the first number:" #READ THE NUMBERS
read a
echo -n "Enter the second number:"
read b
c=`expr $a \* $b`
echo "The result is "$c #PRINT THE RESULT
;;

#DIVISION
4) echo -n "Enter the first number:" #READ THE NUMBERS
read a
echo -n "Enter the second number:"
read b
if [ $b -eq 0 ] #CHECK b IF ZERO
then
echo "Unable to divide by zero"
else
c=`expr $a / $b`
r=`expr $a % $b`
if [ $r -eq 0 ] #CHECK REMAINDER IF ZERO
then
echo "The result is "$c #PRINT THE RESULT
else
echo "The result is "$c #PRINT THE RESULT WITH REMAINDER
echo "The remainder is "$r
fi #END OF IF
fi #END OF IF
;;
*) echo "Invalid Choice"
esac #END OF CASE

Reverse command line argument

y=$#
rev=" "
while [ $y -gt 0 ]
do
rev=$1" "$rev
shift
y=`expr $y - 1`
done
echo $rev

Sample 1

clear
if [ "$1" = "ONE" ] ; then
echo -e "\033[1m"
elif [ "$1" = "TWO" ] ; then
echo -e "\033[7m"
elif [ "$1" = "THREE" ] ; then
echo -e "\033[4m"
else
echo "invalid argument"
fi

Selection sort
echo -n "Enter the number of nos:"
read n
echo "Enter the list of numbers:"
for((i=0;i<n;i++))
do
read a[$i]
done

for((f=0;f<n;f++))
do
min=${a[$f]}
pos=$f
for((i=f+1;i<n;i++))
do
if [ $min -gt ${a[$i]} ]
then
min=${a[$i]}
pos=$i
fi
done
j=`expr $f + 1`
echo "After Pass $j:"
echo "Min=" $min

temp=${a[$f]}
a[f]=$min
a[pos]=$temp

for((j=0;j<n;j++))
do
echo -n " ${a[$j]}"
done
echo " "
echo " "
done

echo "The sorted list is:"


for((i=0;i<n;i++))
do
echo -n " ${a[$i]}"
done

echo " "

Sequence

echo -n "Enter the length:"


read l
i=1
while [ $i -le $l ]
do
k=1
while [ $k -le $i ]
do
echo -n $i" "
k=`expr $k + 1`
done
echo -e "\n"
i=`expr $i + 1`
done

Series
#Series:1+2+3+....+n
echo -n "Enter the value of n:" #READ THE VALUE OF THE LENGTH OF THE
SERIES
read n

#INITIALIZATION
i=1
s=0
while [ $i -le $n ]
do
s=`expr $s + $i` #UPDATE s
i=`expr $i + 1` #INCREASE i
done #END OF WHILE
echo "Sum="$s #PRINT THE RESULT

Str UNIX
for((i=1;i<=4;i++))
do
e=`expr substr unix 1 $i`
echo $e
done

Time Wishes

h=`date +%H`
m=`date +%M`
if [ $h -ge 3 -a $h -le 11 -a $m -le 59 ]
then
echo "Good Morning"
elif [ $h -ge 12 -a $h -le 17 -a $m -le 59 ]
then
echo "Good Afternoon"
elif [ $h -ge 18 -a $h -le 19 -a $m -le 59 ]
then
echo "Good Evening"
else
echo "Good Night"
fi

File size Equal

echo -e -n "\n\n\t Enter the name of the first file:"


read f1
echo -e -n "\n\t Enter the name of the second file:"
read f2
cmp $f1 $f2>/dev/null
if [ $? -eq 0 ]; then
echo -e "\n\z\t $f1 and $f2 are equal"
rm f2
else
echo -e "\n\t\t $f1 and $f2 are not equal"
fi
Factorial using function
function fact()
{
n=$1
f=1
while [ $n -gt 0 ]
do
f=`expr $f \* $n`
n=`expr $n - 1`
done
echo $f
}
echo -e -n "\n\t Enter a non negative integer:"
read n
r=`fact $n`
echo "$n!=$r"

Factorial using recursion

# Factorial using recursion

fac()
{
if [ "$1" -gt 1 ] ; then
next=`expr $1 - 1`
rec=`fac $next`
prod=`expr $1 \* $rec`
echo $prod
else
echo 1
fi
}

echo "Enter a number:"


read num
echo "$num!=`fac $num`"

Find Files of a given range of Size

# This program finds names within a given range which will be


# provided as command line argument

clear
if [ $# -eq 2 ];then
m=$1
n=$2

echo -e "\n\t Find files within the range from $m to $n...\n"


ls -l > [Link]
v=`wc -l [Link] | cut -d ' ' -f 1`
v=`expr $v - 1`

tail -n $v [Link] > [Link]


while read line
do

a=`echo $line | tr -s ' ' | cut -d ' ' -f 5`


b=`echo $line | tr -s ' ' | cut -d ' ' -f 9`

if [ $a -ge $m -a $a -le $n ];then


echo -e "\t\t$b -->$a\n"
fi

done < [Link]


else
echo "Invalid number of inputs..."
fi

Modify calendar

#write shell script to modify the 'cal' command to accept more than one
month

clear
if [ $# -ge 2 ];then
if [ "$1" = "cal" ];then
a=`date`
b=`echo $a | cut -d ' ' -f 6`
shift

while [ $# -gt 0 ]
do
if [ $1 -ge 1 -a $1 -le 12 ];then
cal $1 $b
else
echo -e "\n\n\t\n $1 is invalid month number....\n"
fi
shift
done
else
echo -e "\n\n\t\t Invalid Inputs.."
fi
else
echo -e "\n\n\t\t Improper number of arguments..."
fi

Peterson number

#check whether a number is peterson number or not


#Example:145=1!+4!+5!

echo -e -n "\n\n\t\t Enter the number:"


read n
m=$n
sum=0
while [ $m -ne 0 ]
do
a=`expr $m % 10`

fact=1
for((i=a;i>=1;i--))
do
fact=`expr $fact \* $i`
done
sum=`expr $sum + $fact`
m=`expr $m / 10`
done
if [ $n -eq $sum ];then
echo -e "\n\n\t\t $n is a peterson number\n"
else
echo -e "\n\n\t\t $n is a not peterson number\n"
fi

Total Marks

# Calculate the total marks from a file and store it in another


#file with the details stored in the previous file

if [ -s [Link] ];then
echo -n "" > [Link]
fi

while read another


do
a=`echo $another | cut -d '|' -f 1`
b=`echo $another | cut -d '|' -f 2`
c=`echo $another | cut -d '|' -f 3`
d=`echo $another | cut -d '|' -f 4`

sum=`expr $b + $c + $d`
echo "$a|$b|$c|$d|$sum" >> [Link]
done < [Link]
echo -e "\n\n\t\t task is completed...\n\n"

User name

# write a shell script to check whether a given user name is valid or


not..

clear
echo -e -n "\n\n\t Enter the user name:"
read name
grep $name /etc/passwd>/dev/null
if [ $? -eq 0 ];then
echo -e "\n\t The user is valid.."
else
echo -e "\n\t The user is not valid.."
fi

You might also like