0% found this document useful (0 votes)
8 views11 pages

Shell Pgms

The document contains multiple shell script examples for various tasks such as displaying the current date and time, checking file permissions, copying files, counting characters in a string, and managing a phonebook. Each script includes user input prompts and conditional statements to perform specific operations. The scripts demonstrate fundamental programming concepts in shell scripting, including loops, conditionals, and file handling.

Uploaded by

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

Shell Pgms

The document contains multiple shell script examples for various tasks such as displaying the current date and time, checking file permissions, copying files, counting characters in a string, and managing a phonebook. Each script includes user input prompts and conditional statements to perform specific operations. The scripts demonstrate fundamental programming concepts in shell scripting, including loops, conditionals, and file handling.

Uploaded by

endzera004
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Date and time-write aprogram to display current date and time, number of users , terminal

name, login date and tim


u=`who am i | cut -f1 -d' '`
d=`who am i |cut -f12 -d' '`
t=`who am i | cut -f13 -d' '`
echo "today is `date + %D`"
echo "current time is `date | cut -f5 -d' ' `"
echo "as of now `who | wc -l` user are login to system "
echo "my details.............."
echo "user name $u"
echo "login date is $d"
echo "current login tym is $t"
Output
date: extra operand `%D'
Try `date --help' for more information.
today is
current time is 14:37:42
as of now 2 user are login to system
my details..............
user name ccst29
login date is 2019-02-04
current login tym is 13:50

19. Write a shell script to Display Banner, calander of given year


echo Enter a word for banner
read b
banner $b
echo Enter a year
read y
cal $y

21. Write a shell script which uses all the file test operators
echo "Enter a file name "
read file
if [ -e $file ]
then
echo "File exists"

if [ -r $file ]
then
echo "File has read access"
else
echo "File does not have read access"
fi

if [ -w $file ]
then
echo "File has write permission"
else
echo "File does not have write permission"
fi

if [ -x $file ]
then
echo "File has execute permission"
else
echo "File does not have execute permission"
fi

if [ -f $file ]
then
echo "File is an ordinary file"
else
echo "This is sepcial file"
fi

if [ -d $file ]
then
echo "File is a directory"
else
echo "This is not a directory"
fi

if [ -s $file ]
then
echo "File size is zero"
else
echo "File size is not zero"
fi

else
echo "File does not exist"
fi

22. Write a shell script to copy the contents of file to another. Input file names through
command line. The copy should not be allowed if second file exists.

if [ -e $1 ]
then
if [ -e $2 ]
then
echo File $2 already exists does not make copy
else
cp $1 $2
fi
else
echo File $1 Does not exists
fi

[Link] a shell script to find number of vowels, consonants, numbers in a given string.
clear
echo "Type any String"
read string

length=`echo $string | wc -c`


nvowels=0
nconsonants=0
ndigits=0

while [ $length -gt 1 ]


do
length=`expr $length - 1`
h=`echo $string | cut -c$length`

case $h in
[AaEeIiOoUu]) nvowels=`expr $nvowels + 1`
;;
[BbCcDdFfGgHhJjKkLlMmNnPpQqRrSsTtVvWwXxYyZz])
nconsonants=`expr $nconsonants + 1`
;;

[0-9]) ndigits=`expr $ndigits + 1`


;;
esac
done

echo "Number of Vowels : $nvowels"


echo "Number of Consonants : $nconsonants"
echo "Number of Digits : $ndigits"

24. Code for Shell script to perform operations like display, list, make directory and copy,
rename, delete

i="y"
while [ $i = "y" ]
do
echo "[Link] current dir"
echo "[Link] the dir"
echo "[Link] a dir"
echo "[Link] a file"
echo "[Link] file"
echo "[Link] file"
echo "[Link]"
echo "Enter your choice"
read ch
case $ch in
1)echo "Current Dir is : "
pwd;;
2)echo "Directories are"
ls;;
3)echo "Enter dir name to create"
read d
mkdir $d
echo "$d Dir is created";;
4)echo "Enter filename from copy"
read f1
echo "Enter filenm2 to be copied"
read f2
cp $f1 $f2
echo "$f2 is copied from $f1 ";;
5)echo "Enter file name to rename"
read f1
echo "Enter new name of file"
read f2
mv $f1 $f2
echo "$f1 is renamed as $f2 ";;
6)echo "Enter any filenm to be delete"
read f1
rm $f1
echo $f1" is deleted";;
*)echo "Invalid choice entered";;
esac
echo "Do u want to continue ? "
read i
if [ $i != "y" ]
then
exit
fi
done

25. Write a shell script to compare two files and remove one of them if they are same

echo "Enter first file name "


read file1
echo "Enter second file name "
read file2
if [ -f $file1 ]
then
if [ -f $file2 ]
then
if [ `cmp $file1 $file2` =='' ]
then
rm $file2
echo "File $file2 Removed "
fi
else
echo File $file2 does not Exists
fi
else
echo File $file1 does not Exist
fi

26. Write a shell script to create a file phonebook and do the following, add name and
phonenumber, display all records , search for a record, delete a record
ch=0
while [ $ch -lt 5 ]
do
clear
echo "1. Add new Record "
echo "2. Display"
echo "3. Search"
echo "4. Delete"
echo "5. Exit"
echo "Enter your choice "
read ch
case $ch in
1) echo Enter name
read name
echo Enter Phone Number
read phone
echo "$name $phone" >> phonebook ;;
2) echo "Name Phone "
cat phonebook;;
3) echo "Enter a name to search "
read sname
nm=`grep $sname phonebook | cut -f2`
echo "Phone number of $sname is $nm";;
4) echo "Enter a name to delete "
read nm
while read line
do

if [ `echo $line | cut -f1 -d' '` != $nm ]


then
echo $line >> temp
fi

done < phonebook


cp temp phonebook
rm temp
echo Record of $nm deleted;;
esac
read
done

27) Write a shell script to accept student number, name, marks in 5 subjects. Find total,
average and grade.
Rules: avg>=90 then grade A
Avg<90 && Avg>=80 then grade B
Avg<80 && Avg>=60 then grade C
Avg<60 && Avg>=40 then grade D
Avg<40 then grade E

echo -n "Enter Regno "


read regno
echo -n "Enter Name "
read sname
for i in 1 2 3 4 5
do
echo -n "Enter mark of subject$i "
read mark[$i]
done
s=0
a=0
for i in 1 2 3 4 5
do
s=`expr $s + ${mark[$i]}`
done
a=`expr $s / 5`
if [ $a -ge 90 ]
then
gr="A"
elif [ $a -ge 80 ]
then
gr="B"
elif [ $a -ge 60 ]
then
gr="C"
elif [ $a -ge 40 ]
then
gr="D"
else
gr="E"
fi

echo "Register number : $regno "


echo "Student Name : $sname "
echo "Total Mark of 5 Subjects : $s "
echo "Average Mark of 5 Subject : $a "
echo "Grade : $gr "
28) Write a shell script to read 10 numbers in a array and sort the number in assending
order

for i in {1..10}
do
echo "Enter a number "
read a[$i]
done

for i in {1..10}
do
for j in {1..10}
do
if [ ${a[$i]} -lt ${a[$j]} ]
then
t=${a[$i]}
a[$i]=${a[$j]}
a[$j]=$t
fi
done
done
echo "Sorted Numbers ......"
echo "====================="
for i in {1..10}
do
echo ${a[$i]}
done

You might also like