[go: up one dir, main page]

0% found this document useful (0 votes)
196 views34 pages

Linux PGM

The document provides examples of shell scripts for various tasks including checking if a number is prime, determining if a number is an Armstrong number, calculating the sum of the first n numbers, and calculating simple interest. It also includes scripts for generating a Fibonacci series, calculating gross salary, checking if a number is even or odd, counting vowels/characters in a file, and monitoring disk space usage. The scripts demonstrate using conditional statements, loops, arithmetic operations, reading/writing files, command line arguments, and sending emails.

Uploaded by

Dr. R. Malathi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
196 views34 pages

Linux PGM

The document provides examples of shell scripts for various tasks including checking if a number is prime, determining if a number is an Armstrong number, calculating the sum of the first n numbers, and calculating simple interest. It also includes scripts for generating a Fibonacci series, calculating gross salary, checking if a number is even or odd, counting vowels/characters in a file, and monitoring disk space usage. The scripts demonstrate using conditional statements, loops, arithmetic operations, reading/writing files, command line arguments, and sending emails.

Uploaded by

Dr. R. Malathi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 34

Write a shell script to check whether a given number is prime or

not.
echo "Enter a number: "
read num
i=2
f=0
while [ $i -le `expr $num / 2` ]
do
if [ `expr $num % $i` -eq 0 ]
then
f=1
f
i=`expr $i + 1`
done
if [ $f -eq 1 ]
then
echo "The number is composite"
else
echo "The number is Prime"
f
------------------------------------------------------------------------------------------------------------Enter a number:
5
The number is Prime
Write a shell script to check whether a given number is an
Armstrong number or not.
echo "Enter a number: "
read c
x=$c
sum=0
r=0
n=0
while [ $x -gt 0 ]
do
r=`expr $x % 10`
n=`expr $r \* $r \* $r`
sum=`expr $sum + $n`
x=`expr $x / 10`
done
if [ $sum -eq $c ]
then

echo "It is an Armstrong Number."


else
echo "It is not an Armstrong Number."
f
------------------------------------------------------------------------------------------------------------OUTPUT
Enter a number:
153
It is an Armstrong Number.
Write a shell script to find the sum of the first n numbers.
echo "Enter a number: "
read num
i=1
sum=0
while [ $i -le $num ]
do
sum=`expr $sum + $i`
i=`expr $i + 1`
done
echo "The sum of frst $num numbers is: $sum"
------------------------------------------------------------------------------------------------------------Enter a number:
5
The sum of frst 5 numbers is: 15
Write a shell script to find the simple interest.
Script-3
echo "Enter the Principle Amount: "
read p
echo "Enter the rate of interest: "
read r
echo "Enter the number of years: "
read n
i=`expr $p \* $r \* $n`
i=`expr $i / 100`
echo "The Simple Interest is :Rs.$i"
------------------------------------------------------------------------------------------------------------Enter the Principle Amount:
5000

Enter the rate of interest:


10
Enter the number of years:
1
The Simple Interest is :Rs.500
Write a shell script to find whether a given number is even or
odd.
read num
if [ `expr $num % 2` -eq 0 ]
then
echo "$num is Even."
else
echo "$num is Odd."
f
------------------------------------------------------------------------------------------------------------Enter a number:
8
8 is Even.
Find Basic Salary,Hra,DA, And Calculate the Net Salary.
echo "Enter Basic Salary: "
read b
hra=0
da=0
if [ $b -gt 1500 ]
then
hra=500
da=`expr $b \* 98 / 100`
else
hra=`expr $b \* 10 / 100`
da=`expr $b \* 90 / 100`
f
gs=`expr $b + $hra + $da`
echo "Gross Salary is: Rs.$gs"
echo "Details:"
echo "Basic: Rs.$b"
echo "HRA: Rs.$hra"
echo "DA: Rs.$da"
------------------------------------------------------------------------------------------------------------Enter Basic Salary:

15000
Gross Salary is: Rs.30200
Details:
Basic: Rs.15000
HRA: Rs.500
DA: Rs.14700
Write a Shell Script to generate Fibonacci Series.(Example : 0 1 1
2 3 5 8 13 ...)
clear
echo "How many number of terms to be generated ?"
read n
x=0
y=1
i=2
echo "Fibonacci Series up to $n terms :"
echo "$x"
echo "$y"
while [ $i -lt $n ]
do
i=`expr $i + 1 `
z=`expr $x + $y `
echo "$z"
x=$y
y=$z
done
Code for Calculate the gross salary in Unix / Linux / Ubuntu
echo -e "Enter ur basic salary \c"
read sal
if [ $sal -ge 1000 ]
then
da=`expr $sal \* 40 / 100`
ha=`expr $sal \* 20 / 100`
Nsal=`expr $sal + $da + $ha`
echo "ur Basic Salary
$sal "
echo "ur Dearness Allowance $da "
echo "Ur House rent
$ha "
echo "
------------"
echo "Ur Net Salary is Rs. $Nsal "else
echo "Pls enter basic salary greater than 1000 "
f

--------------------------------------------------------------------------------$ q1
Enter ur basic salary 2000
ur Basic Salary
2000

Write a shell script that, given a file name as the argument


will count vowels, blank spaces, characters, number of line and
symbols.

#!/bin/bash
fle=$1
v=0
if [ $# -ne 1 ]
then
echo "$0 fleName"
exit 1
fi
if [ ! -f $fle ]
then
echo "$fle not a fle"
exit 2
fi
while read -n 1 c
do
l=$(echo $c | tr [:upper:] [:lower:])
[[ "$l" == "a" || "$l" == "e" || "$l" == "i" || "$l" == "o" || "$l" == "u" ]]
&& (( v++ ))
done < $fle
echo
echo
echo
echo

"Vowels : $v"
"Characters : $(cat $fle | wc -c)"
"Blank lines : $(grep -c '^$' $fle)"
"Lines : $(cat $fle|wc -l )"

Write a shell script that, given a file name as the argument


will count English language articles such As 'A', 'An' and 'The'.

#!/bin/bash
fle=$1

a=0
if [ $# -ne 1 ]
then
echo "$0 fleName"
exit 1
fi
if [ ! -f $fle ]
then
echo "$fle not a fle"
exit 2
fi
while read line
do
l=$(echo $line | tr [:upper:] [:lower:])
for word in $l
do
[[ $word == "a" || $word == "an" || $word == "the" ]] && ((a+
+))
done
done < $fle
echo "articles : $a"

Write a shell script that, given a file name as the argument


will write the even numbered line to a file with name evenfile
and odd numbered lines in a text file called oddfile.

#!/bin/bash
fle=$1
counter=0
if [ $# -ne 1 ]
then
echo "$0 fleName"
exit 1
fi
if [ ! -f $fle ]
then
echo "$fle not a fle"

exit 2
fi
while read line
do
((counter++))
EvenNo=$(( counter%2 ))
if [ $EvenNo -eq 0 ]
then
echo $line >> evenfle
else
echo $line >> oddfle
fi
done < $fle

Write a shell script to monitor Linux server disk space using


a while loop. Send an email alert when percentage of used disk
space is >= 90%.

#!/bin/bash
ADMIN="me@somewher.com"
# set alert level 90% is default
ALERT=90
df -H | grep -vE '^Filesystem|tmpfs|cdrom' | awk '{ print $5 " " $1 }' |
while read output;
do
#echo $output
usep=$(echo $output | awk '{ print $1}' | cut -d'%' -f1 )
partition=$(echo $output | awk '{ print $2 }' )
if [ $usep -ge $ALERT ]; then
echo "Running out of space \"$partition ($usep%)\" on $(hostname) as
on $(date)" |
mail -s "Alert: Almost out of disk space $usep" $ADMIN
fi
done

Write a shell script to determine if an input number is a


palindrome or not. A palindromic number is a number where

the digits, with decimal representation usually assumed, are


the same read backwards, for example, 58285.

#!/bin/bash
echo -n "Enter number : "
read n
# store single digit
sd=0
# store number in reverse order
rev=""
# store original number
on=$n
while [ $n -gt 0 ]
do
sd=$(( $n % 10 )) # get Remainder
n=$(( $n / 10 )) # get next digit
# store previous number and current digit in reverse
rev=$( echo ${rev}${sd} )
done
if [ $on -eq $rev ];
then
echo "Number is palindrome"
else
echo "Number is NOT palindrome"
fi

Write a shell program to read a number *such as 123) and


find the sum of digits (1+2+3=6).

#!/bin/bash
#store the no
num=$1
#store the value of sum
sum=0

if [ $# -ne 1 ]
then
echo "$0 number"
exit 1
fi
while [ $num -gt 0 ]
do
digit=$(( num%10 ))
num=$(( num/10 ))
sum=$(( digit+sum ))
done
echo "Sum of digits = $sum"

Write a shell program to read a number and display reverse


the number. For example, 123 should be printed as as 321.

#!/bin/bash
#store the no
num=$1
#store the reverse number
rev=0
if [ $# -ne 1 ]
then
echo "$0 number"
exit 1
fi
while [ $num -gt 0 ]
do
digit=$(( num%10 ))
num=$(( num/10 ))
rev=$(( digit + rev*10 ))
done
echo "Reverse of number = $rev"

Write the shell program which produces a report from the


output of ls -l in the following format:

fle1
fle2
[DIR] test/
Total regular fles : 7
Total directories : 4
Total symbolic links : 0
Total size of regular fles : 2940
#!/bin/bash
#copying the out of ls -l command to a file
ls -l > /tmp/tmp.tmp
#initilizing values
sum=0
dir=0
fle=0
link=0
#reading the file
while read line
do
#getting the first character of each line to check the type of file
read -n 1 c <<< $line
#checking if the file is a directory or not
if [ $c == "d" ]
then
((dir++))
echo "[DIR] ${line}/" | cut -d" " --felds="1 9" >>
/tmp/dir.tmp
elif [ $c == "-" ] #true if the file is a regular file
then
((file++))
echo $line | cut -d" " -f8 >> /tmp/fle.tmp
elif [ $c == "l" ] #true if the file is a symbolic link
then
((link++))

fi
size=$( echo $line | cut -d" " -f5 ) #getting the size of the file
sum=$(( sum+size )) #adding the size of all the files
done < /tmp/tmp.tmp
cat /tmp/fle.tmp #output the name of all the files
cat /tmp/dir.tmp #output the name of all the directory
echo
echo
echo
echo

"Total
"Total
"Total
"Total

regular fles = $fle"


directories = $dir"
symbolic links = $link"
size of regular fle = $size"

#removing the temporary files


rm /tmp/fle.tmp
rm /tmp/dir.tmp
rm /tmp/tmp.tmp

Write a shell script that will count the number of files in


each of your sub-directories using the for loop.

#!/bin/bash
START=$HOME
# change your directory to command line if passed
# otherwise use home directory
[ $# -eq 1 ] && START=$1
if [ ! -d $START ]
then
echo "$START not a directory!"
exit 1
fi
# use find command to get all subdirs name in DIRS variable
DIRS=$(find "$START" -type d)
# loop thought each dir to get the number of files in each of subdir
for d in $DIRS
do

[ "$d" != "." -a "$d" != ".." ] && echo "$d dirctory has $(ls -l $d | wc -l)
fles"
done

Write a shell script that accepts two directory names as


arguments and deletes those files in the first directory which
are similarly named in the second directory.

#!/bin/bash
SRC="$1"
DST="$2"
if [ $# -ne 2 ]
then
echo "$(basename $0) dir1 dir2"
exit 1
fi
if [ ! -d $SRC ]
then
echo "Directory $SRC does not exists!"
exit 2
fi
if [ ! -d $DST ]
then
echo "Directory $DST does not exists!"
exit 2
fi
for f in $DST/*
do
#echo Processing $f
if [ -f $f ]
then
tFile="$SRC/$(basename $f)"
if [ -f $tFile ]
then
echo -n "Deleting $tFile..."
/bin/rm $tFile
[ $? -eq 0 ] && echo "done" || echo "failed"

fi
fi
done

Write a shell script to search for no password entries in


/etc/passwd and lock all accounts.

#!/bin/bash
# Shell script for search for no password entries and lock all accounts
# Set your email
ADMINEMAIL="admin@somewhere.com"
### Do not change anything below ###
#LOG File
LOG="/root/nopassword.lock.log"
STATUS=0
TMPFILE="/tmp/null.mail.$$"
echo "-------------------------------------------------------" >>$LOG
echo "Host: $(hostname), Run date: $(date)" >> $LOG
echo "-------------------------------------------------------" >>$LOG
# get all user names
USERS="$(cut -d: -f 1 /etc/passwd)"
# display message
echo "Searching for null password..."
for u in $USERS
do
# find out if password is set or not (null password)
passwd -S $u | grep -Ew "NP" >/dev/null
if [ $? -eq 0 ]; then # if so
echo "$u" >> $LOG
passwd -l $u #lock account
STATUS=1 #update status so that we can send an email
fi
done
echo
"============================================
============" >>$LOG
if [ $STATUS -eq 1 ]; then

echo "Please see $LOG fle and all account with no password are
locked!" >$TMPFILE
echo "-- $(basename $0) script" >>$TMPFILE
mail -s "Account with no password found and locked" "$ADMINEMAIL" <
$TMPFILE
# rm -f $TMPFILE
fi

Write a shell program to read two numbers and display all


the odd numbers between those two numbers.

#!/bin/bash
# Shell program to read two numbers and display all the odd
echo -n "Enter frst number : "
read n1
echo -n "Enter second number : "
read n2
if [ $n2 -gt $n1 ];
then
for(( i=$n1; i<=$n2; i++ ))
do
# see if it is odd or even number
test=$(( $i % 2 ))
if [ $test -ne 0 ];
then
echo $i
fi
done
else
echo "$n2 must be greater than $n1, try again..."
fi
Write a shell script to arrange numbers in ascending or
descending order as per the user choice
echo enter the no of element
read n1
for (( i=0; i<$n1; i++ ))

do
echo enter `expr $i + 1` the element.
read a[$i]
done
for (( i=0; i<$n1; i++ ))
do
for (( j=`expr $i + 1`; j<$n1; j++ ))
do
if [ ${a[$i]} -gt ${a[$j]} ]
then
x=${a[$i]}
a[$i]=${a[$j]}
a[$j]=$x
f
done
done
echo 1.Ascending 2.Descending
echo enter your choice...
read c
if [ $c = 1 ]
then
echo the ascending order is....
for (( i=0; i<$n1; i++ ))
do
echo ${a[$i]}
done
elif [ $c = 2 ]

then
echo the descending order is...
for (( i=$n1; i>0; i-- ))
do
echo ${a[`expr $i - 1`]}
done
else
echo wrong choice......
f
Code for menu driven shell program which have the options
change the term, change the prompt and user list of unix
system. in Unix / Linux / Ubuntu
count = 0
while [ $count -eq 0 ]
do
tput clear
echo "1. Change the term "
echo "2. Change the prompt "
echo "3. User list of unix system "
echo "4. Quit "if [ $choice -eq 1 ]
then
elif [ $choice -eq 2 ]
elif [ $choice -eq 3 ]
$who
else
count = 1
exit
f
done

Unix Shell Programs


Program 1
Write a shell script to display the calendar of the current month
and current year.

echo "The calendar of the present month is "


echo "Present date"
date
echo "calendar of present month"
cal `date "+%m 20%y"`
Program 2
Write a shell script to interactively get the file name and the
pattern to be searched and finally display only the lines that
matches.
echo "Enter the pattern to be searched:"
read pname
echo "Enter the fle to be used:"
read fname
echo "Searching for $pname from fle $fname"
echo "Selected records are"
grep "$pname" $fname

Program 3
Write a menu driven program in shell script to check all the
numeric comparison between the variables for each and every
option
echo "Numeric Comparison"
echo "Enter two numbers to do numeric comparison"
read a
read b
echo
echo
echo
echo
echo
echo
echo

""
"Menu"
"1.Equal to"
"2.Not equal to"
"3.Greater than"
"4.Greater than or equal to"
"5.Lesser than"

echo "6.Lesser than or equal to"


echo "7.exit"
read choice
case $choice in
1) if [ $a -eq $b ] ;
then
echo "the numbers
f
;;
2) if [ $a -ne $b ]
then
echo "the numbers
f
;;
3) if [ $a -gt $b ]
then
echo "the numbers
else
echo "the numbers
f
;;
4) if [ $a -ge $b ]
then
echo "the numbers
else
echo "the numbers
f
;;
5) if [ $a -lt $b ]
then
echo "the numbers
else
echo "the numbers
f
;;
6) if [ $a -le $b ]
then
echo "the numbers
else
echo "the numbers
f

$a and $b are equal"

$a and $b are not equal"

$a is greater than $b"


$b is greater than $a"

$a is greater than or equal $b"


$b is greater than or equal $a"

$a is lesser than $b"


$b is lesser than $a"

$a is lesser than or equal $b"


$b is lesser than or equal $a"

;;
7) exit ;;
esac
Program 4
Write a shell script to get the pattern, input filename and output
filename and retrieve the lines of matching pattern and store it in
the output file, check the conditions like input strings are not null
and not equal
echo " program to store the output in a fle "
echo "Get the input from command line"
if test $# -ne 3
then
echo "you have not keyed in 3 arguments"
echo "syntax is sh fle.sh pattern inputfle outputfle"
exit 3
else
if grep "$1" $2 > $3
then
echo "Pattern found"
else
echo "Pattern not found"
f
echo "Contents of output fle"
cat $3
f
Program 5
Write a shell script to get a filename and check all file related
comparisons like file exists, readable and writable etc
echo " File related exist status"
echo "Enter the fle name"
read fname
echo
echo
echo
echo

""
"File related test are listed below"
"1.Existence"
"2.Regular fle"

echo
echo
echo
echo
echo

"3.Readable fle"
"4.Writable fle"
"5.executable fle"
"6.directory"
"7. fles size greater than zero"

echo "The status of the fle $fname is given below "


if [ -e $fname ] ; then
echo "File exists"
f
if [ -f $fname ] ; then
echo "File is a regular fle"
f
if [ -r $fname ] ; then
echo "File is a readable fle"
f
if [ -w $fname ] ; then
echo "File is a writable fle"
f
if [ -x $fname ] ; then
echo "File is an executable fle"
f
if [ -d $fname ] ; then
echo "$fname is a directory"
f
if [ -s $fname ] ; then
echo "File size is greater than zero"
f
Program 6
Write a menu driven shell program that does the following
operations
a. listing the files
b. process used by user
c. todays date
d. users of the system

e. current path of the directory


f. word count of a file
g. type of a file
h. quit to shell
echo
echo
echo
echo
echo
echo
echo
echo
echo

Menu to do the following options


1. listing the fles
2. process used by user
3. todays date
4. users of the system
5. current path of the directory
6. word count of a fle
7. type of a fle
8. quit to shell

echo "Enter the choice"


read choice
case $choice in
1)echo "listing of the fles"
ls -l ;;
2)echo "processes used by user"
ps -f ;;
3)echo "today's date"
date ;;
4)echo "user's "
who ;;
5)echo "current working directory"
pwd ;;
6)echo "enter flename"
read fname
echo "wordcount of the fle"
wc $fname ;;
7)echo "enter flename"
read fname
echo "File type"
fle $fname ;;
8) exit ;;
esac

done
Program 7
Write a shell script to that retrieve the information from the shell
command to display the full format of date. ( no. of days present
in the current month using case )
echo Display date in full format
d = `date +%d`
m = `date +%m`
y = `date +%y`
echo $d - $m 20$y
echo the no. of days in each month
case $m in
1) mm = January
nn = 31 ;;
2) mm = February
nn=28 ;;
3) mm = March
nn=31 ;;
4) mm = April
nn=30 ;;
5) mm = May
nn=31 ;;
6) mm = June
nn=30 ;;
7) mm = July
nn=31 ;;
8) mm = August
nn=31 ;;
9) mm = September
nn=30 ;;
10) mm = October
nn=31 ;;
11) mm = November
nn=30 ;;
12) mm = December
nn=31 ;;
esac

echo the current month is $mm the no. of days in this month is $nn
Program 8
Write a menu driven program using shell script to do all the
arithmetic operations
echo Menu driven program to do all arithmetic operations
while true; do
echo Enter two numbers a and b
read a
read b
echo
echo
echo
echo
echo
echo
echo

Menu Arithmetic Operation


1.Addition
2.Subtraction
3.Multiplication
4.Division
5.Modulo
6.exit

echo enter the choice


read choice
case $choice in
1) echo Addition of 2 numbers - $a and $b is
c=`expr $a + $b`
echo $c ;;
2) echo Subtraction of 2 nos- $a and $b is
c=`expr $a - $b`
echo $c ;;
3) echo Multiplication of 2 nos- $a and $b is
c=`expr $a \* $b`
echo $c ;;
4) echo Division of 2 nos- $a and $b is
c=`expr $a / $b`
echo $c ;;
5) echo Mod of 2 nos- $a and $b is
c=`expr $a % $b`
echo $c ;;
esac
done

Program 9
Write a menu driven program to do the following :
a. copy one file to another
b. move one file to another
c. remove a file
d. create a file
e. compare two files
f. difference between two files
g. link two files
h. exit to shell
echo Menu fle comparisons
while true ; do
echo Name of frst fle
read f1
echo Name of second fle
read f2
echo Menu
echo "1. copy one fle to another
echo "2. move one fle to another
echo "3. remove a fle
echo "4. create a fle
echo "5. compare two fles
echo "6. difference between two fles
echo "7. link two fles
echo "8. exit to shell
echo "Enter the choice"
read choice
case $choice in
1) echo "copy one fle to another"
echo The source fle - $f1 and destination fle - $f2
cp $f1 $f2
cat $f1
cat $f2 ;;
2) echo "move one fle to another"
echo The source fle - $f1 and destination fle - $f2
mv $f1 $f2
cat $f1

cat $f2 ;;
3) echo "remove a fle "
echo The fle to remove - $f1
rm $f1
cat $f1;;
4) echo "create a fle"
echo The fle to be created- $f1
cat > $f1 ;;
5) echo "compare two fles "
echo The 1st fle - $f1 and 2nd fle - $f2
cmp $f1 $f2 ;;
6) echo "difference between fles"
echo The 1st fle - $f1 and 2nd fle - $f2
diff $f1 $f2 ;;
7) echo "link 2 fles"
echo The 1st fle - $f1 and 2nd fle - $f2
ln $f1 $f2
cat >>$f1
cat $f2 ;;
8) exit ;;
esac
done
Program 10
Write a shell script to list all the files greater than 250 bytes.
for fle in *
do
if [ -f $fle ] ; then
x=`ls -l $fle | awk '{print $5}' `
if [ $x -gt 250 ]
then
echo the fle is $fle and bytes : $x
f
f
done
Program 11.
Give salutation like good morning, good afternoon depends
upon the current time.

str=` date "+%H" `


if [ $str -lt 10 ]
then
echo "Good morning"
elif [ $str -lt 15 ]
then
echo "Good Afternoon"
elif [ $str -lt 19 ]
then
echo "Good Evening"
else
echo "Good Night"
f
Program 12.
Write a shell script to print the top 5 users
Echo "Print top 5 users"
who | head -5 > ouptutfle
cat outputfle

Program 13.
Write a shell script to check whether the input is the number or
the character.
echo Check for number or a character/string
echo Enter the input string or number
read ip
if [ -z $ip ] ; then
echo You have not entered the String ; exit 1
f
if [ -n $ip ] ; then
echo The entered input is a string/character
else
echo The entered input is a numerical character
f

Program 14.
Write a shell script to check whether the person is logged in or
not
echo "login name of ur friend"
read l
until who | grep $l
do sleep 60;
done
echo $1 is now logged in
Program 15.
Write a shell script to print the number of lines and words in a
file.
echo print the number of lines and words in a fle
echo enter the name of the fle
read fname
l = wc l $fname
w = wc w $fname
c = wc c $fname
echo Number of lines present in the fle $fname - $l
echo Number of words present in the fle $fname - $w
echo Number of characters present in the fle $fname - $c
Program 16.
Write a shell script to give the count and display the number of
subdirectories, ordinary files present in the current directory. Do
it separately.
echo "Display the directories and fles seperately"
for fle in *
do
if [ -f $fle ] ; then
echo $fle >> flist
elif [ -d $fle ] ; then
echo $fle >>dirlist
f

done
echo "the list of fles and their count"
cat flist
echo "Count - `wc -l flist`"

echo "the list of subdirectories and their count"


cat dirlist
echo "Count - `wc -l dirlist`"
Program 17.
Write a shell script to display the UID, GID, login directory of a
given user.
echo " Enter Login id "
read login
grep $login /etc/passwd > temp
ud=`cut -f 3 -d ":" temp`
gd=`cut -f 4 -d ":" temp`
ld=`cut -f 6 -d ":" temp`
echo uid is $ud
echo gid is $gd
echo working directory is $ld

Program 18.
Write a shell script to find out the sum of digits in a five digit
number
echo Sum of digits in a fve digit number
echo enter the fve digit number :
read n
i=0
a = $n
while [ $a ge 0 ] ; do
$i = ` expr $a % 10 `

$sum = ` expr $sum + $i `


$a = ` expr $a / 10 `
done
echo the sum of digits in a fve digit number $n - $sum
Program 19.
Write a shell script to sum the first N integers
echo Summation of frst N numbers
echo enter the number:
read n
sum=0
i=1
while [ $i le $n ] ; do
$sum = ` expr $sum +$i `
$i = ` expr $i + 1 `
done
echo The sum of frst $n natural numbers - $sum
Program 20.
Write a shell script to list all the files in the current directory
which contain a particular word.
echo "enter the pattern"
read pattern
echo "enter the outputfle"
read flist
echo "List of fles that contain the same name as the pattern in a
directory"
ls | grep "$pattern"
echo "Count - `ls |grep "$pattern" |wc -l`"
echo " "
echo " --------------------------"
echo " "

echo "List of fles which contain the pattern in its fle content"
for fle in *
do
if grep "$pattern" $fle > /dev/null
then
echo $fle >> $flist
f
done
cat flist
echo "Count - `wc -l $flist`"
Program 21
Write a shell script to print the Fibonacci series
echo Fibonacci Series
echo Enter the number :
read n
i=2
a=0
b=1
echo Fibonacci Series upto $n terms :
echo $a
echo $b
while [ $i le $n ] ; do
c = ` expr $a + $b `
echo $c
$i = ` expr $i + 1 `
done
Program 22
Write a shell script that takes a command line argument of option
and kilometer. If option is m ( convert kilometers to meters) and
option is d ( convert kilometer to decimeter) , finally if option is
c (convert kilometer to centimeter)
Ex: sh file.sh m 23
Result should be : 23000 meters
echo "Conversion Program"
k=$2

if [ $# -ne 2 ]
then
echo "You have not keyed in 2 arguments"
exit 3
else
if [ $1 = "-m" ]
then
ans=`echo $k \* 1000 | bc`
echo "$ans meters"
elif [ $1 = "-d" ]
then
ans=`echo $k \* 10000 | bc`
echo "$ans decimeters"
elif [ $1 = "-c" ]
then
ans=`echo $k \* 100000 | bc`
echo "$ans centimeters"
f
f
~
Program 23
Write a shell script to check whether the input number is odd or
even
echo Check for odd or even
echo Enter the number :
read n
i = ` expr $n % 2 `
if [ $i eq 0 ] ; then
echo the entered number $n is an even number
else
echo the entered number $n is an odd number
f
Program 24
Write a shell script to comparison of 3 numbers and print the
greatest and the smallest
echo "Comparison of 3 nos"

echo "enter the three numbers a,b,c"


read a
read b
read c
if [ $a -gt $b ] && [ $a -gt $c ]
then
echo "A - $a is greatest"
elif [ $b -gt $a ] && [ $b -gt $c ]
then
echo "B - $b is greatest"
else
echo "C - $c is greatest"
f
Program 25
Write a shell script to check whether the given year is leap or not
echo "Finding Leap Year"
echo "enter any year"
read y
a=`expr $y % 4`
b=`expr $y % 100`
c=`expr $y % 400`
if [ $a -eq 0 -a $b -ne 0 -o $c -eq 0 ]
then
echo "$y is a leap year "
else
echo "$y is not a leap year "
f

Program 26
Write a shell script to get the basic salary of a person and
calculate his gross salary, whose da is 40% of basic, and hra is
20% of basic, pf is 10% of basic. Hint: gs = basic + da + hra pf
echo Calculation of Gross Salary
echo Enter the basic salary of a person
read bp

da= ` echo $bp * 0.4 | bc`


hra = ` echo $bp * 0.2 | bc`
pf = ` echo $bp *0.1 | bc`
gs = ` echo $bp + $da + $hra - $pf | bc`
echo the gross salary of the person is $gs
Program 27
Write a shell script using cases to calculate the area and
perimeter of a rectangle and to calculate the area and
circumference of the circle, Get the respective input from the
user.
echo the details about rectangle and circle
while true ; do
echo Menu
echo 1.Rectangle
echo 2.Circle
echo Enter the choice
read ch
case $ch in
1) echo enter the length and breadth of a rectangle:
read l
read b
area = ` echo $l * $b | bc`
sum=`echo $l +$b | bc`
peri = `echo 2 * $sum | bc`
echo The area of the rectangle - $area
echo The perimeter of the rectangle - $peri ;;
2) echo enter the radius of the circle
read r
area = ` echo 3.14 * $r *$r | bc`
cir = ` echo 0.5 * $area | bc`
echo The area of the circle - $area
echo The circumference of the rectangle - $cir ;;
3) exit ;;

esac
done
Program 28.
Write a shell script to calculate the simple interest for three sets
for p, n, r using loops
echo Simple interest for 3 set
i=1
while [ $i le 3]
do
echo enter the
echo enter the
read p
echo enter the
read n
echo enter the
read r

details of person $i
principle amount:
no, of years :
rate of interest:

s= `echo $p * $n *$r | bc` #for floating point values


echo The Simple interest for $i person - $s
done

You might also like