0% found this document useful (0 votes)
78 views32 pages

Raish Vhora: K.P.Patel School of Management & Computer Studies (KSMCS)

The document provides instructions and examples for using various Linux commands from the terminal. It lists 25 common commands (date, ls, who, cal, ps, etc.) and provides the syntax and sample output for each one. It also includes examples of shell scripts that perform tasks like arithmetic operations on input numbers, checking if a string is a palindrome, determining if a number is even/odd and calculating digit sums, replacing strings, finding file modification times, copying files in reverse order between directories, finding file paths, and copying files between directories with common files being overwritten.

Uploaded by

Rubina
Copyright
© Attribution Non-Commercial (BY-NC)
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)
78 views32 pages

Raish Vhora: K.P.Patel School of Management & Computer Studies (KSMCS)

The document provides instructions and examples for using various Linux commands from the terminal. It lists 25 common commands (date, ls, who, cal, ps, etc.) and provides the syntax and sample output for each one. It also includes examples of shell scripts that perform tasks like arithmetic operations on input numbers, checking if a string is a palindrome, determining if a number is even/odd and calculating digit sums, replacing strings, finding file modification times, copying files in reverse order between directories, finding file paths, and copying files between directories with common files being overwritten.

Uploaded by

Rubina
Copyright
© Attribution Non-Commercial (BY-NC)
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
You are on page 1/ 32

qwertyuiopasdfghjklzxcvbnmqwerty

uiopasdfghjklzxcvbnmqwertyuiopasd
fghjklzxcvbnmqwertyuiopasdfghjklzx
K.P.Patel School of Management &
cvbnmqwertyuiopasdfghjklzxcvbnmq
Computer Studies (KSMCS)
Programming Skills-V(OS)
wertyuiopasdfghjklzxcvbnmqwertyui
opasdfghjklzxcvbnmqwertyuiopasdfg
Raish Vhora
hjklzxcvbnmqwertyuiopasdfghjklzxc
vbnmqwertyuiopasdfghjklzxcvbnmq
wertyuiopasdfghjklzxcvbnmqwertyui
opasdfghjklzxcvbnmqwertyuiopasdfg
hjklzxcvbnmqwertyuiopasdfghjklzxc
vbnmqwertyuiopasdfghjklzxcvbnmq
wertyuiopasdfghjklzxcvbnmqwertyui
opasdfghjklzxcvbnmqwertyuiopasdfg
hjklzxcvbnmrtyuiopasdfghjklzxcvbn
mqwertyuiopasdfghjklzxcvbnmqwert
yuiopasdfghjklzxcvbnmqwertyuiopas
K.P.Patel School of Management & Computer Studies MCA
(KSMCS)

1. Check the output of the following commands.


date, ls, who, cal, ps, wc, cat, uname, pwd, mkdir, rmdir, cd, cp, rm, mv, diff, chmod,
grep, sed, head, tail, cut, paste, sort, find.
1) date
date - print or set the system date and time
OUTPUT
digit@ubuntu:~$ date
Thu Dec 9 00:14:12 EST 2010

2) ls
ls - list directory contents
OUTPUT
digit@ubuntu:~$ ls
Desktop Downloads Firefox_wallpaper.png Pictures Templates VLC
Documents examples.desktop Music

3) who
who - show who is logged on
OUTPUT
digit@ubuntu:~$ man who
who
who
digit tty7 2010-12-08 23:36 (:0)
digit pts/0 2010-12-09 00:12 (:0.0)

4) cal
cal, ncal — displays a calendar and the date of Easter
OUTPUIT
digit@ubuntu:~$ cal
December 2010
Su Mo Tu We Th Fr Sa
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31

5) ps
ps - report a snapshot of the current processes.
OUTPUT
digit@ubuntu:~$ ps

2 Raish Vhora
K.P.Patel School of Management & Computer Studies MCA
(KSMCS)

PID TTY TIME CMD


3008 pts/0 00:00:00 bash
3025 pts/0 00:00:00 man
3035 pts/0 00:00:00 pager
3041 pts/0 00:00:00 man
3051 pts/0 00:00:00 pager
3216 pts/0 00:00:00 man
3226 pts/0 00:00:00 pager
3236 pts/0 00:00:00 man
3246 pts/0 00:00:00 pager
3256 pts/0 00:00:00 man
3266 pts/0 00:00:00 pager
3276 pts/0 00:00:00 man
3285 pts/0 00:00:00 pager
3292 pts/0 00:00:00 ps

6) wc
wc - print newline, word, and byte counts for each file
digit@ubuntu:~/VLC$ wc install.sh
33 89 603 install.sh

7) cat
cat - concatenate files and print on the standard output
digit@ubuntu:~/VLC$ cat f1.txt
Nadiad
Anand
Baroda

8) uname
uname - print system information
digit@ ubuntu:~/VLC$ uname
Linux

9) pwd
pwd - print name of current/working directory
digit@ubuntu:~/VLC$ pwd
/home/digit/VLC

10) mkdir
mkdir - make directories
digit@ubuntu:~/VLC$ mkdir KSMCS

3 Raish Vhora
K.P.Patel School of Management & Computer Studies MCA
(KSMCS)

11) rmdir
rmdir - remove empty directories
digit@ubuntu:~/VLC$ rmdir KSMCS

12) cd
cd – to change the current directory
digit@ubuntu:~/VLC$ cd KSMCS

13) cp
cp - copy files and directories
digit@ubuntu:~/VLC$ cp f1.txt ff.txt

14) rm
rm - remove files or directories
digit@ubuntu:~/VLC$ rm KSMCS

15) mv
mv - move (rename) files
mv KSMCS MCA

16) diff
diff - compare files line by line
digit@ubuntu:~/VLC$ diff f1.txt f2.txt

17) chmod
chmod - change file mode bits
digit@ubuntu:~/VLC$ chmod +ux prg1.sh

18) grep
grep, egrep, fgrep, rgrep - print lines matching a pattern
grep -c $i temp

19) sed
sed - stream editor for filtering and transforming text
sed -n "$eno"'!p' $fnm > te.lst

20) head
head - output the first part of files
digit@ubuntu:~/VLC$ head f1.txt

4 Raish Vhora
K.P.Patel School of Management & Computer Studies MCA
(KSMCS)

Nadiad
Anand
Baroda

21) tail
tail - output the last part of files
digit@ubuntu:~/VLC$ tail f1.txt
Nadiad
Anand
Baroda

22) cut
cut - remove sections from each line of files
cut -d ' ' -f1

23) paste
paste - merge lines of files
paste f1.txt f2.txt

24) sort
sort - sort lines of text files
digit@ubuntu:~/VLC$ sort f1.txt
Anand
Baroda
Nadiad

25) find
find - search for files in a directory hierarchy
digit@ubuntu:~/VLC$ find f1.txt ff

2. Write shell script


a) Accept numbers and perform addition, subtraction, division and ultiplication.
echo "Enter a First number:"
read a
echo "Entre a Second number:"
read b
echo "+"
echo "-"
echo "*"
echo "/"
echo "Enter a choice:"

5 Raish Vhora
K.P.Patel School of Management & Computer Studies MCA
(KSMCS)

read ch
if [ "$ch" = "+" ];
then
echo "$a + $b =" `expr $a + $b` ;
elif [ "$ch" = "-" ];
then
echo "$a - $b =" `expr $a - $b` ;
elif [ "$ch" = "*" ];
then
echo "$a * $b =" `expr $a \* $b` ;
elif [ "$ch" = "/" ];
then
echo "$a / $b =" `expr $a / $b` ;
fi
b) Accept the string and checks whether the string is palindrome or not.
clear
echo "enter string:- \c"
read s
l=${#s}
echo "length of string is:- "$l
rev=""
while [ $l -ge 1 ]
do
ch=`echo $s | cut -c$l`
rev=$rev$ch
l=`expr $l - 1`
done
echo "reverse is" $rev

if [ $rev = $s ]
then
echo "palindrom"
else
echo "not palindrom"
fi
c) Accept number and check the number is even or odd, finds the length of the
number, sum of the digitsin the number.
clear
echo "Enter The Number :\c"
read n
l=${#n}
echo "\nThe Length Of The Nunber Is : "$l

6 Raish Vhora
K.P.Patel School of Management & Computer Studies MCA
(KSMCS)

t=`expr $n % 2`
if [ $t -eq 0 ]
then
echo "The Number Is Even"
else
echo "The Number Is Odd"
fi
while [ $n -gt 0 ]
do
r=`expr $n % 10`
sum=`expr $sum + $r`
n=`expr $n / 10`
done

echo "Sum Of Digit Is :"$sum

d) Accept strings and replace a string by another string.


clear
echo "Enter The Original String :\c"
read s

echo "Enter The String For The Replace :\c"


read s1

if [ $s = $s1 ]
then
echo "No Need For Change"
else
temp=$s
s=$s1
s1=$temp
fi
echo "The Old String Is :"$s

e) Accept filename and displays last modification time if file exists, otherwise
display appropriate message.
clear
echo "Enter The File Name :\c"
read name
if [ -f $name ]
then
t=`ls -l $name | tr -s ' ' | cut -d ' ' -f6,7`

7 Raish Vhora
K.P.Patel School of Management & Computer Studies MCA
(KSMCS)

echo Modification time of file is $t


else
echo "Does Not Exit"
fi
f) Fetch the data from a file and display data into another file in reverse order.
clear
echo "\n Enter The Directory Name : \c"
read dname

l=`cat $dname | wc -l`


rm reverse
while [ $l -gt 1 ]
do
str=`head -$l $dname | tail -l`
echo $str > temp
rev=`rev temp`
echo $rev >> reverse
l=`expr $l -l`

done
cat reverse

3. Write a script to find the global complete path for any file.
clear
echo "enter file name:- "
read fname

echo "file path is:- "

find / -name $fname

4. Write a script to broadcast a message to a specified user or a group of users


logged on any terminal.
clear
echo "========================="
echo "W R I T E - M E S S A G E"
echo "========================="
echo "[1] Create Message "
echo "[2] To Single User"
echo "[3] To All User"
echo "[4] Exit"
echo "========================="

8 Raish Vhora
K.P.Patel School of Management & Computer Studies MCA
(KSMCS)

echo "Enter Your Choice:- \c"


read ch
case $ch in
1) echo "Write Message [Press Ctrl+d to terminate]"
cat > message
;;
2) echo "Enter User Name:- \c"
read uname
write $uname < message
;;
3)
who | cut -d ' ' -f1 | uniq > temp
set `cat temp`
for i in $*
do
write $i < message
done
rm temp
;;
4)
exit
;;
esac
5. Write a script to copy the file system from two directories to a new directory in
such a way that only the latest file is copied in case there are common files in both
the directories.
clear
echo "enter source directory 1 :- \c"
read dir1

if [ ! -d $dir1 ]
then
echo "$dir1 directory does not exists"
exit
fi

echo "enter source directory 2 :- \c"


read dir2

if [ ! -d $dir2 ]
then
echo "$dir2 directory does not exists"

9 Raish Vhora
K.P.Patel School of Management & Computer Studies MCA
(KSMCS)

exit
fi

echo "enter destination directory 3:- \c"


read dir3

if [ ! -d $dir3 ]
then
echo "$dir3 directory does not exists"
exit
fi

set `ls $dir1`


`ls $dir2 > temp`

for i in $*
do
c=`grep -c $i temp`
if [ $c -eq 0 ]
then
cp "$dir1/$i" $dir3
else
if [ "$dir1/$i" -nt "$dir2/$i" ]
then
echo "$dir1/$i copied to $dir3"
cp "$dir1/$i" $dir3
else
echo "$dir2/$i copied to $dir3"
cp "$dir2/$i" $dir3
fi
fi
done
6. Write a script to compare identically named files in two different directories
and if they are same, copy one of them in a third directory.
clear
echo "enter source directory 1 :- \c"
read dir1

if [ ! -d $dir1 ]
then
echo "$dir1 directory does not exists"
exit

10 Raish Vhora
K.P.Patel School of Management & Computer Studies MCA
(KSMCS)

fi

echo "enter source directory 2 :- \c"


read dir2

if [ ! -d $dir2 ]
then
echo "$dir2 directory does not exists"
exit
fi

echo "enter destination directory 3:- \c"


read dir3

if [ ! -d $dir3 ]
then
echo "$dir3 directory does not exists"
exit
fi

set ` ls $dir1 `
` ls $dir2 > temp `

for i in $*
do
c=` grep -c $i temp `
if [ $c -eq 1 ]
then
c=` cmp "$dir1/$i" "$dir2/$i" | wc -l `
if [ $c -eq 0 ]
then
echo "$dir1/$i copied to $dir3"
cp "$dir1/$i" $dir3
fi
fi
done
7. Write a script to delete zero sized files from a given directory (and all its sub-
directories).
$ find . -empty -maxdepth 1 -exec rm {} \;
to find zero size file
find . -size 0c

11 Raish Vhora
K.P.Patel School of Management & Computer Studies MCA
(KSMCS)

8. Write a script to display the name of those files (in the given directory) which
are having multiple links.
clear
echo "enter directory name:- \c"
read dname

if [ -d $dname ]
then
`ls -l $dname | tr -s ' ' | cut -d ' ' -f2,8 > temp`
l=`cat temp | wc -l`
i=2
while [ $i -le $l ]
do
str=`head -$i temp | tail -l`
link=`echo $str | cut -d ' ' -f1`
fname=`echo $str | cut -d ' ' -f2`
if [ $link -gt 1 ]
then
echo "file $fname has $link link"
fi
i=`expr $i + 1`
done
else
echo "not exist"

fi
9. Write a script to display the name of all executable files in the given directory.
clear
echo "enter directory home:- \c"
read dname

if [ -d $dname ]
then
set `ls $dname`

for i in $*
do
if [ -x $i ]
then
echo $i
fi
done

12 Raish Vhora
K.P.Patel School of Management & Computer Studies MCA
(KSMCS)

else
echo "file does not exist"
fi

or

clear

echo "\n Enter The Directory :\c"


read dname

if [ -d $dname ]
then
ls $dname
for i in $*
do
if [ -x $i ]
then
echo $i
ls $i
fi
done

else
echo "\n Does Not Exits"

fi

10.Write a script to display the date, time and a welcome message (like Good
Morning etc.). The time should be displayed with “a.m.” or “p.m.” and not in 24
hours notation.
month=`date +%B`
day=`date +%d`
year=`date +%Y`
hour=`date +%k`
minutes=`date +%M`
if [ $hour -gt 12 ]
then
var="p.m"
else
var="a.m"
fi

13 Raish Vhora
K.P.Patel School of Management & Computer Studies MCA
(KSMCS)

if [ $hour -gt 0 -a $hour -lt 12 ]


then
echo "Good Morning"
elif [ $hour -ge 12 -a $hour -lt 16 ]
then
echo "Good Afternoon"
elif [ $hour -ge 16 -a $hour -lt 19 ]
then
echo "Good Evening"
else
echo "Good Night"
fi
if [ $hour -gt 12 ]
then
hour=`expr $hour - 12`
fi
echo "Date is "$day-$month-$year
echo "Time is "$hour : $minutes $var

11. Write a script to display the directory in the descending order of the size of
each file.
clear

echo "enter directory:- \c"


read dname

if [ -d $name ]
then
echo "file in descending order"
echo "size <-> filename"
`ls -l $dname | tr -s ' ' | cut -d ' ' -f5,8 > temp`
sort -grk1 temp
else
echo "directory does not exist"
fi

12. Write a script to implement the following commands:


Tree (of DOS) which (of UNIX)
echo "______________use tree _____________ "
tree -d | head -20
echo "_______________use which_____________"
echo "ENTER THE COMMAND WE CAN FIND PATH"

14 Raish Vhora
K.P.Patel School of Management & Computer Studies MCA
(KSMCS)

read a
which $a

13. Write a script for generating a mark sheet after reading data from a file.
File contains student roll no, name , marks of three subjects.
if [ $# -ne 3 ]
then
echo "You have to Enter 3 arguments for marks....."
else
m1=$1
m2=$2
m3=$3

sum=`expr $m1 + $m2 + $m3`


avg=`expr $sum / 3`
echo "\nTotal is --> "$sum
echo "Average marks are --> " $avg
if [ $avg -ge 70 ]
then
echo "\n\nStudent has achieved Distinction..."
elif [ $avg -ge 60 and -lt 70 ]
then
echo "\n\nStudent has achieved First class..."
elif [ $avg -ge 50 and -lt 60 ]
then
echo "\n\nStudent has achieved Second class..."
elif [ $avg -ge 40 and -lt 50 ]
then
echo "\n\nStudent has achieved Third class..."
else
echo "\n\nStudent failed..."
fi
fi
14. Write a script to make following file and directory management operations
menu based:
Display current directory
List directory
Make directory
Change directory
Copy a file
Rename a file
Delete a file
Edit a file

15 Raish Vhora
K.P.Patel School of Management & Computer Studies MCA
(KSMCS)

clear

echo "\n 1 >> Display current directory :"


echo "\n 2 >> List directory :"
echo "\n 3 >> Make directory :"
echo "\n 4 >> Change directory :"
echo "\n 5 >> Copy a file :"
echo "\n 6 >> Rename a file :"
echo "\n 7 >> Delete a file :"
echo "\n 8 >> Edit a file :"

echo "\n Enter Your Choice :\c"


read ch

case $ch in

1)
dir;;

2)
ls;;

3)
echo "\n Enter Your Directory Name :\c"
read dname
mkdir $dname;;

4)
cd;;

5)

echo "\n Enter The Source File Name :\c"


read name

echo "\n Enter The Destination File Name :\c"


read dname

mv -i $name $dname;;

6)

16 Raish Vhora
K.P.Patel School of Management & Computer Studies MCA
(KSMCS)

echo "\n Enter The Old File Name :\c"


read name

echo "\n Enter The New File Name :\c"


read name

mv -i $name $dname;;

7)

echo "\n Enter The File For Delete :\c"


read name

rmdir $name;;

*)
echo"Invalid Choice :"

esac
15. Write a script which reads a text file and output the following
Count of character, words and lines.
File in reverse.
Frequency of particular word in the file.
Lower case letter in place of upper case letter.
clear

echo "\nEnter The File Name :\c"


read fname

if [ -f $fname ]
then

echo "\n= = = = = = = = = = = = = = = = = = = = \n"


echo "\n\t Menu Driven \n"
echo "\n\t= = = = = = = = = = = = = = = = = = = = \n"
echo "\n\t 1 >> Count of character, words and lines.\n"
echo "\n\t 2 >> File in reverse.\n"

17 Raish Vhora
K.P.Patel School of Management & Computer Studies MCA
(KSMCS)

echo "\n\t 3 >> Frequency of particular word in the file.\n"


echo "\n\t 4 >> Lower case letter in place of upper case letter.\n"
echo "\n\t 5 >> Exit\n"
echo "\n\t= = = = = = = = = = = = = = = = = = = = \n"

echo "\n Enter Your Choice . . . \c"


read c

case $c in

1)

c=`cat $fname | wc -c`


l=`cat $fname | wc -l`
w=`cat $fname | wc -w`

echo "\n The Number of Character In FIle Is : "$c


echo "\n The Number Of Line In File Is : "$l
echo "\n The Number Of Word In File Is :"$w ;;

2)
rev $fname;;

3)

echo "\n Enter The Word :\c"


read word

t=`grep -wc $word $fname`

echo "\n The Number Of Word In File Is Available "$t "Times";;

4)
echo " Lower Into UpperCase :"
t=`cat $fname | tr "a-z" "A-Z"`
echo $t

18 Raish Vhora
K.P.Patel School of Management & Computer Studies MCA
(KSMCS)

echo " \n UpperCase Into LowerCase:"


q=`cat $fname | tr "A-Z" "a-z" `
echo $q;;

5)exit;;
esac

else
echo "\n File Does Not Exit "
fi
16. Write a shell script to check whether the named user is currently logged in or
not.
echo "enter the user name:- \c"
read unm

c=`who -u | grep -wc $unm`


if [ $c -eq 0 ]
then
echo "$unm is not log in"
else
echo "$unm is log in"
fi

17. Write a Script for Simple Database Management System Operation.


Database File Contains Following Fields.
EMP_NO
EMP_NAME
EMP_ADDRESS
EMP_AGE
EMP_GENDER
EMP_DESIGNATION
EMP_BASIC_SALARY

Provide Menu Driven Facility For

VIEW RECORD BASED ON QUERY


ADD RECORD
DELETE RECORD
MODIFY RECORD.
COUNT TOTAL NUMBER OF RECORDS
EXIT

19 Raish Vhora
K.P.Patel School of Management & Computer Studies MCA
(KSMCS)

fnm=emp.lst

while echo "Operating On Employee Data " ;do

echo " ####### Menu #######"

echo "1. View File Data"

echo "2. View Record"

echo "3. Add REcord"

echo "4. Delete Record"

echo "5. Modify Record"

echo "6. Exit"

echo " ###################"

echo "Select your Choice :- \c "

read ch

case "$ch" in

1)
cat $fnm
;;

2)

while echo "Enter The Emp No :- \c " ;do

read eno

set `cut -d "|" -f1 $fnm |grep -c "$eno"`

if [ $1 -ne 0 ] ; then

cut -d "|" -f1-7 $fnm | grep "$eno"

20 Raish Vhora
K.P.Patel School of Management & Computer Studies MCA
(KSMCS)

break

else

echo "Record Not Found"

continue

fi

done
;;

3)

while echo "Enter Emp No :- \c " ;do

read eno

if [ `cut -d "|" -f1 $fnm |grep -c "$eno"` -eq 1 ] ;then

echo "Emp No is already Exist"

continue

else

break

fi

done

echo "Enter Emp Name :- \c "

read ename

echo "Enter Addr :- \c "

read addr

21 Raish Vhora
K.P.Patel School of Management & Computer Studies MCA
(KSMCS)

echo "Enter age :- \c "

read age

echo "Enter Gender :- \c "

read gend

echo "Enter Designation :- \c "

read desg

echo "Enter Salary :- \c "

read sal

echo "$eno|$ename|$addr|$age|$gend|$desg|$sal" >> $fnm


;;

4)

while echo "Enter the Emp No :- \c" ;do

read eno

set `cut -d "|" -f1 $fnm| grep -c "$eno"`

if [ $1 -ne 0 ] ; then

echo "Are you sure do u want to delete :- \c"

read ans

case "$ans" in

[yY]*)

cut -d "|" -f1-7 $fnm| grep -v "$eno" > te.lst

#sed -n "$eno"'!p' $fnm > te.lst

22 Raish Vhora
K.P.Patel School of Management & Computer Studies MCA
(KSMCS)

cp te.lst $fnm

esac

break

else

echo "Record Not Found"

continue

fi

done
;;

5)

echo "\n Enter Emp No. :- \c"

read eno

echo "What u want to change :- "

echo "1.name \n2.Addr \n3.age \n4.desg \n5.salary"

echo "Enter Your Choice \c"

echo "Enter Your Choice \c"

read cho

echo "`grep -n "$eno" $fnm`" > line

lno=`cut -d ":" -f1 line`

case "$cho" in

1) echo "Enter Name :- \c"

23 Raish Vhora
K.P.Patel School of Management & Computer Studies MCA
(KSMCS)

fild=2;;

2) echo "Enter Address :- \c "

fild=3;;

3) echo "Enter Age :- \c"

fild=4;;

4) echo "Enter Desgination :- \c"

fild=6;;

5) echo "Enter Salary :- \c"

fild=7;;

6)

break;;

*) echo "Invalid choice"

fild=0;;

esac

if [ $fild -ne 0 ] ;then

read new

old=`cut -d "|" -f$fild line`

sed "$ln"'s/'"$old"'/'"$new"'/' $fnm > te.lst

cp te.lst $fnm

fi
;;

24 Raish Vhora
K.P.Patel School of Management & Computer Studies MCA
(KSMCS)

esac

done

18. Write A Script To Perform Following String Operations Using Menu:


COMPARE TWO STRINGS.
JOIN TWO STRINGS.
FIND THE LENGTH OF A GIVEN STRING.
OCCURRENCE OF CHARACTER AND WORDS
E. REVERSE THE STRING.
clear

echo "\n\t = = = = = = = = = = = = = = = = = ="


echo "\n\t\t String Manipulation "
echo "\n\t = = = = = = = = = = = = = = = = = ="
echo "\n\t\t 1 >> COMPARE TWO STRINGS."
echo "\n\t\t 2 >> JOIN TWO STRINGS."
echo "\n\t\t 3 >> FIND THE LENGTH OF A GIVEN STRING."
echo "\n\t\t 4 >> OCCURRENCE OF CHARACTER AND WORDS"
echo "\n\t\t 5 >> REVERSE THE STRING."
echo "\n\t\t 6 >> Exit"
echo "\n\t = = = = = = = = = = = = = = = = = ="
echo "\n\t\t Enter Your Choice :\c"
read ch

case $ch in

1)

echo "\n Enter The First String :\c "


read str

echo "\n Enter The Second String :\c "


read str1

if [ $str = $str1 ]
then
echo "Both Strings Are Same "
else
echo "Both Strings Are Different"
fi

25 Raish Vhora
K.P.Patel School of Management & Computer Studies MCA
(KSMCS)

;;

2)
echo "\n Enter The First String :\c "
read s1

echo "\n Enter The Second String :\c "


read s2

str3=$s1$s2
echo $str3

;;
3)
echo "\n Enter The String :\c "
read str
l=${#str}
echo "\n The Length Of String Is "$l
;;
4)

echo "\n Enter The String :\c "


read str

c=`echo $str | wc -c`


echo "\n The Number Of The Character Is :"$c

w=`echo $str | wc -w`


echo "The Occurence Of the Word Is :"$w

;;
5)
echo "\n Enter The String :\c "
read s

l=${#s}

rev=""

while [ $l -ge 1 ]
do
26 Raish Vhora
K.P.Patel School of Management & Computer Studies MCA
(KSMCS)

ch=`echo $s | cut -c $l`


rev=$rev$ch
l=`expr $l - 1`
done

echo "reverse string is : "$rev

;;
6)exit;;

esac
19. Write a script to calculate gross salary for any number of employees
Gross Salary =Basic + HRA + DA.
HRA=10% and DA= 15%.
clear

echo "\n How Many Numbers You Want :\c"


read no

while [ $no -gt 0 ]


do
echo "\n Enter Basic Salary :\c"
read bs

hra=`expr $bs \* 10`


hra=`expr $hra / 100`

da=`expr $bs \* 15`


da=`expr $da / 100`

gs=`expr $bs + $hra + $da`

echo "\n Gross Salary Is "$gs


no=`expr $no - 1`
done
20. Write a script to check whether a given string is palindrome or not.
clear
echo "enter string:- \c"

27 Raish Vhora
K.P.Patel School of Management & Computer Studies MCA
(KSMCS)

read s
l=${#s}
echo "length of string is:- "$l
rev=""
while [ $l -ge 1 ]
do
ch=`echo $s | cut -c$l`
rev=$rev$ch
l=`expr $l - 1`
done
echo "reverse is" $rev

if [ $rev = $s ]
then
echo "palindrom"
else
echo "not palindrom"
fi
21. Write a script to check whether a given number is palindrome or not.
clear

echo "\n Enter The Number :\c"


read no
t=$no
rev=0
while [ $no -gt 0 ]
do
r=`expr $no % 10`
rev=`expr $rev \* 10`
rev=`expr $rev + $r`
no=`expr $no / 10`
done

echo "\n The Original Number Is :"$t


echo "\n The Revarse Number Is :"$rev

if [ $rev -eq $t ]
then
echo palindrom
else
echo not palindrom
fi
28 Raish Vhora
K.P.Patel School of Management & Computer Studies MCA
(KSMCS)

22. Write a script to display all words of a file in ascending order.


echo "Enter File Name \c"
read fname

if [ -f $fname ]
then
echo "==================================================="
echo "Original File Content is "
echo "==================================================="
cat $fname
echo "==================================================="
echo "File words in ascending order is"
echo "==================================================="
set `cat $fname`
for i in $*
do
echo "$i" >> temp
done
sort temp
rm temp
echo "==================================================="
else
echo "File does not exist"
fi
23. Write a script to display all lines of a file in ascending order.
echo "Enter File Name \c"
read fname

if [ -f $fname ]
then
echo "==================================================="
echo "Original File Content is "
echo "==================================================="
cat $fname
echo "==================================================="
echo "File lines in ascending order is"
echo "==================================================="
cat $fname | sort
else
echo "File does not exist"
fi
24. Write a script to display the last modified file.

29 Raish Vhora
K.P.Patel School of Management & Computer Studies MCA
(KSMCS)

clear
fname=`ls -lt | tr -s ' ' | head -2 | tail -1 | cut -d ' ' -f8`
ctime=`ls -lt | tr -s ' ' | head -2 | tail -1 | cut -d ' ' -f6,7`

echo Last modified File is $fname


echo Last File Modification time is $ctime

25. Write a shell script to add the statement #include <stdio.h> at the beginning of
every C source file in current directory containing printf and fprintf.
while true; do
read -e -p "Enter Directory: " path || exit
[[ -d $path ]] && break
echo "Invalid path! Try Again!"
done
path=${path%/}
myargs=`grep -l -e "printf" -e "fprintf" $path/*.c | xargs`

if [ $? -gt 1 ]; then # grep exits with status 1 when no matches were found.
echo -n "No Matches were found. " && exit
fi
temp=$(mktemp tmp.XXXXXXXXX)
for i in $myargs # Here, grep has the exit status 0.
do
echo "Do you want to add '#include <stdio.h>' to $i?"
read S
case $S in
Y|y|YES|Yes|yes|yeah)
sed '1i\
#include<stdio.h>' "$i" > "$temp" # i for insertion, 1 for 1st line. $i is the file to
insert. and all output will be redirected to $temp

mv "$temp" "$i" # renaming $temp by over writing to $i

;;

n|N|NO|no|No|nope)
echo "Alright! Next.."
shift
;;

*)
echo "Invalid input."

30 Raish Vhora
K.P.Patel School of Management & Computer Studies MCA
(KSMCS)

;;
esac

done

if [ -z $myargs ]; then
echo "No Matches were found. Try another Directory"
else
clear
head -n5 $path/*.c | less
fi

rm $temp

26. Write a script that behaves both in interactive and non-interactive mode.
When no arguments are supplied, it picks up each C program from current
directory and lists the first 10 lines. It then prompts for deletion of the file. If the
user supplies arguments with the script, then it works on those files only.
clear
if [ $# -eq 0 ]
then
set `ls *.c`
for i in $*
do
if [ -f $i ]
then
echo "file name:- $i"
head 10 $i | cat
echo "do you want delete file $i [y/n]:- \c"
read ans
if [ $ans = 'Y' -o $ans = 'y' ]
then
rm $i
fi
echo "enter any key to continue"
read ans
fi
done

else
for i in $*

31 Raish Vhora
K.P.Patel School of Management & Computer Studies MCA
(KSMCS)

do
if [ -f $i ]
then
echo "file name:- $i"
head 10 $i | cat
echo "do you want delete file $i [y/n]:- \c"
read ans
if [ $ans = 'Y' -o $ans = 'y' ]
then
rm $i
fi
echo "enter any key to continue"
read ans
fi
done
fi
27. Write a script that deletes all leading and trailing spaces in all lines in a file.
Also remove blank lines from a file. Locate lines containing only printf but not
fprintf.
echo “Enter the file name”
read a'

b=$(echo ${$a} | sed -e 's/^ *//g;s/ *$//g')

echo "a->[${$a}] b->[${b}]"

OR

sed 's/^[ \t]*//;s/[ \t]*$//' yourfilename

32 Raish Vhora

You might also like