Shell Programming
1. Write a shell program to add two numbers:
Code:
echo "Enter a number"
read a
echo "Enter another number"
read b
c=`expr $a + $b`
echo "The result $c"
2. Write a shell program to check weather a number is even or odd:
Code:
echo "Enter a number"
read n
temp=`expr $n % 2`
if [ $temp -eq 0 ]
then
echo "Even"
else
echo "Odd"
fi
3. Write a shell program to convert a number from decimal to binary:
Code:
echo "Enter a dacimal number"
read dec
bin=`echo "obase=2;ibase=10;$dec"| bc`
echo "The binary number is $bin"
4. Write a shell program to add << in beginning and >> in end of every line in a text
file:
Code:
clear
echo "Enter file name"
read file
rm temp
touch temp
nol=`cat $file | wc -l`
i=1
while [ $i -le $nol ]
do
line=`cat $file | head -$i | tail -1`
modln="<< $line >>"
echo "$modln" >> temp
i=`expr $i + 1`
done
cat temp
5. Check file or directory:
for x in *
do
if [ -d $x ]
then
echo "$x -------------> Directory"
elif [ -f $x ]
then
if [ -r $x ]
then
echo -n "R"
fi
if [ -w $x ]
then
echo -n "W"
fi
if [ -x $x ]
then
echo -n "X"
fi
echo "------ $x ------------------> File"
fi
done
6. different type of loops:
Number 1:
for x in 1 2 3 4 5 6 hello
do
echo "$x"
done
Number 2:
for x in *
do
echo "$x"
done
Number 3:
i=1
while [ $i -le 5 ]
do
echo "$i"
i=`expr $i + 1`
done
7. Write a shell program to calculate salary.
Code:
echo "Enter basic salary"
read bsal
da=`echo "scale=4;$bsal*0.75"| bc`
hra=`echo "scale=4;$bsal*0.15" | bc`
tsal=`echo "scale=4;$bsal+$da+$hra" | bc`
echo "Total salary is $tsal"
8. Write a shell program to find the longest and shortest line in a file.
Code:
clear
echo "Enter file name"
read file
HLineC=`cat $file | head -1 | wc -c`
LLineC=$HLineC
HLine=`cat $file | head -1`
LLine=$HLine
nol=`cat $file | wc -l`
i=2
while [ $i -le $nol ]
do
line=`cat $file | head -$i | tail -1`
noc=`echo "$line" | wc -c`
if [ $noc -gt $HLineC ]
then
HLineC=$noc
HLine=$line
fi
if [ $noc -lt $LLineC ]
then
LLineC=$noc
LLine=$line
fi
i=`expr $i + 1`
done
echo "Longest line is: $HLine"
echo "Sortest line is: $LLine"
9. Write a shell program to check weather a number is prime or not from command
line.
n=$1
count=`factor $n | wc -w`
if [ $count -eq 2 ]
then
echo "Number is prime"
else
echo "Number is not prime"
fi
10. Write a shell program to display only prime numbers from command line.
num=$#
i=1
while [ $i -le $num ]
do
n=$1
count=`factor $n | wc -w`
if [ $count -eq 2 ]
then
echo "$n "
fi
shift 1
i=`expr $i + 1`
done
11. Write a shell program to display only prime numbers from data file that only contain
numbers.
fileName=$1
rm temp1
cat $fileName | tr ' ' '\n' > temp1
cat temp1 | grep -v "^$" > temp2
mv temp2 temp1
count=`cat temp1 | wc -l`
i=1
while [ $i -le $count ]
do
number=`cat temp1 | head -$i | tail -1`
n=`factor $number | wc -w`
if [ $n -eq 2 ]
then
echo "$number "
fi
i=`expr $i + 1`
done
12. Write a shell program to display only prime numbers from data file that contain
numbers and words.
fileName=$1
rm temp1
cat $fileName | tr -cd "[0-9 ]" | tr -s ' ' > temp1
cat temp1 | tr ' ' '\n' | grep -v "^$" > temp2
mv temp2 temp1
line=`cat temp1 | wc -l`
i=1
while [ $i -le $line ]
do
number=`cat temp1 | head -$i | tail -1`
n=`factor $number | wc -w`
if [ $n -eq 2 ]
then
echo "$number "
fi
i=`expr $i + 1`
done