Thursday, January 10, 2013

Shell Scripting Program

1-. Write a Shell Script that takes a search string and file name from the terminal & displays the results.

read a
read b
word= ‘grep $a $b’
if test ‘echo $word |wc –c’-eq1
then
echo “patern not found”
else
grep $a $b
fi

output-

sh ss1
enter the string to be searched
ashs
enter the file name
ss2

patern not found

2- Write a Shell Script that takes pattern and file name as command line arguments and displays the results appropriately i.e. pattern found/pattern not found.

if test $# -ne 2
then
echo “Invalid no. of arguments”
else
word=’grep $1 $2
if test ‘word | wc –c’ –eq1
then
echo ”pattern not found ”
else
grep $1 $2
echo “pattern found”
fi
fi

output

sh ss2 contents ss12
echo “ \n” content are same second file is being deleted
echo “ \n” content are different

pattern found

3- Write a Shell Script that accepts only three arguments from the command line. The first argument is the pattern string, the second argument is the file name in which the pattern is to be searches and the third argument is the file name in which the result is to be stored.

if test $# -ne3
then echo “Invalid no. of arguments”
else
grep $1 $2 | cat>$3
if test –s $3
then
echo “pattern found”
cat $3
else
echo “pattern not found”
fi

output:

sh ss3 echo ss12 output
pattern found
echo “\n ” content are same second file is being deleted
echo “ \n” content are different
echo “ \n” file does not exist
cat output
echo “ \n” content are different
echo “ \n” file does not exist.

4- Write a Shell Script that accepts a file name as a command line argument and finds out if its a regular file or a directory. If its a regular file, then performs various tests to see if it is readable, writable, executable etc.

if [-d $1]
then
echo It’s a directory
elif  [-f $1]
then
echo file exist
if [-r $1]
then
echo file has read permission
echo the content are ----
cat $1
else
file don’t have read permission
fi
if [-w $1]
then
echo “ ”file have write permission
cat >>$1
else
echo you do not have write permission
fi
if [-x $1]
then
echo “ ” file have execute permission
else
you don’t have execute permission
fi
else
echo Nothing exist by this name
fi

Output:

$ sh ss4 free
file exist
file has read permission
the content are _ _ _ _ _
Ashish is a mca student
__ _ _ _ _ _ _ _

You have execute permission

5- Write a Shell Script that computes the factorial of a given number

echo “Enter the no. to compute its factorial”
read num
i=1
fact=1
while test $i –le $num
do
fact = “factorial of : $num is: $fact”

Output:

 sh ss6
Enter the no. to compute its factorial
6

Factorial of :6 is: 720

6- Write a Shell Script that works like a calendar reminding the user of certain things depending on the day of the week.

a= `date + % A`
echo “\n” welcome Ashish
echo “\n ” Today is $a
echo your task for today is as follows
case $a in
Monday) echo complete ur Monday task
;;
Tuesday) echo complete ur Tuesday task
;;
Wednesday) echo complete ur Wednesday task
;;
Thursday) echo complete ur Thursday task
;;
Friday) echo complete ur Friday task
;;
Saturday) echo complete ur Saturday task
;;
Sunday) echo complete ur Sunday task
;;
Esac

Output:

Sh ss7
Welcome ashish
Today is Sunday
Complete ur Sunday taske

7- Write a Shell Script that changes the extension of a group of files from txt to doc

echo Before
ls *.txt
for i in `ls *.txt`
domv $i `echo $i|cut -f1 -d"."`.doc
done
echo After
ls *.doc

Output :

$sh ss8
Before
d.txt f.txt r.txt
After
d.doc  e?q?w.doc  f.doc  q?w.doc  r.doc  w.doc

8-  Write a Shell Script that accepts both file name and a set of patterns as positional parameters to a script.

 fn=$1
shift
for i in $*
do
grep $i
$fn
done

Output:

ashish@ASHISH-PC:~/File$ sh SS9 ss8 txt doc
ls *.txt
for i in `ls *.txt`
mv $i `echo $i|cut -f1 -d"."`.doc
ls *.doc
ashish@ASHISH-PC:~/File$

9- Write a Shell Script which will redirect the output of the date command without the time into a file.

echo Enter the file name
read file
a=`date|cut -b 1-11,25-28`
echo $a|tee -a $file
clear
echo "\n"$file sucessfully created
echo "\n""Content of file is :"`cat $file`

Output :

sh ss10
Enter the file name
ss2
Thu Nov 20 2008
ss2 sucessfully created

Content of file is :if test $# -ne 2 then echo "Invalid no. of arguments" else word=`grep $1 $2` if test `echo $word|wc -c` -eq 1 then echo "Pattern not found" else grep $1 $2 fi fi Thu Nov 202008

10- Write a Shell Script (using while loop) to execute endlessly (until terminated by user) a loop which displays contents of current directory, disk space status, sleep for 30 seconds and display the users currently logged in on the screen.

char=y
while [ $char ="y" ]
do
ls
df -t
sleep 30
who
echo"Want to continue\(y/n\)?"
read char
done


Output :

sh ss11
cold gold sold ss11 ss14  SS17 ss2 ss22 SS25 SS28 SS30 ss6 SS9
e.txt  output  ss1  ss12  ss15  ss18  SS20 ss23 SS26 SS29 ss4 ss7 tr.c
f1 q.txt ss10 ss13 ss16 SS19 ss21 SS24 SS27 ss3 SS5 ss8 w.txt
Filesystem       1K-blocks         Used                Available  Use%  Mounted on
/dev/sda6        4845056          2476544          2124328    54%      /
varrun                         452316            96                    452220        1%      /var/run
varlock            452316            0                      452316         0%     /var/lock
udev                452316            56                    452260          1%     /dev
devshm            452316           12                    452304            1%   /dev/shm
lrm                  452316           39760              412556            9%   /lib/modules/2.6.24-19-generic/volatile
gvfs-fuse-daemon 4845056    2476544          2124328          54%     /home/ashish/.gvfs
ashish tty7 2008-11-20 11:20 (:0)
ashish pts/0 2008-11-20 11:25 (:0.0)
Want to continue\(y/n\)?

11- Write a Shell Script that receives two file names as arguments. It should check whether content of the two files is same or not. If they are same, second file should be deleted.

if [ -f $1 -a -f $2 ]
then
if diff $1 $2
then
cat $1
echo "\n"
cat $2
echo "\n" Contents are same Second file is being deleted
rm $2
else
echo"\n" Contents are different
fi
else
echo "\n" File does not exist
fi

Output:

$ sh ss12 df rt
Ashish
Ashish
Contents are same Second file is being deleted
$

12 - If a number is input through the keyboard, WASS to calculate sum of its digits.

echo Enter a no.
read num
sum=0
while true
do
if test `expr $num % 10` -gt 0
then
temp=`expr $num % 10`
sum=`expr $sum + $temp`
num=`expr $num / 10`
else
echo $sum
exit
fi

Output:

sh ss13
Enter a no.
2345
14

13-  Write a Shell Script that performs a count-down either from 10 (default) or from the value that is entered by the user.

echo "Enter the Countdown time."
read n
clear
while [ $n -ge 0 ]
do
echo $n
sleep 1
n=`expr $n – 1`
done
echo Count down timer stopped

Output:

sh ss14
Enter the Countdown time.
3
3
2
1
0
Count down timer stopped

14- Write a Shell Script which takes a command line argument of Kms and by default converts that number into meters. Also provide options to convert km to dm and km to cm.

Km=$1
mt=`expr $km \* 1000`
echo "1.) km to dm"
echo "2 ) km to cm"
echo Enter your choice
read num
case $num in
1)dm=`expr $km \* 10000`
echo $km in meters is :$mt and in decimeters is : $dm
;;
2)cm=`expr $km \* 100000`
echo $km in meters is :$mt and in centimeters is : $cm
;;
esac

Output:

sh ss15 5
5 kms in meters is 5000
km to dm
km to cm
Enter your choice
1
5 in meters is- 5000 and in decimeters is 50000

15-  Write a Shell Script using for loop, which displays the message "Welcome to the UNIX System".

for var in $*
do
echo "Welcome to Unix System"
shift 1
done

Output:

sh ss16
Welcome to Unix System

16- Write a Shell Script to change the file name of all files in a directory from lower-case to upper-case.

for i in *
do
mv $i `echo $i|tr "[:lower:]" "[:upper:]"`
done

Output:

sh SS17
mv: `COLD' and `COLD' are the same file
mv: `E.TXT' and `E.TXT' are the same file
mv: `F1' and `F1' are the same file
mv: `GOLD' and `GOLD' are the same file
mv: `Q.TXT' and `Q.TXT' are the same file
mv: `SOLD' and `SOLD' are the same file
mv: `SS1' and `SS1' are the same file
COLD GOLD SOLD SS11 SS14 SS17 SS2 SS22 SS25 SS28 SS30 SS6 SS9
E.TXT OUTPUT SS1 SS12 SS15 SS18 SS20 SS23 SS26 SS29 SS4 SS7 TR.C
F1 Q.TXT SS10 SS13 SS16 SS19 SS21 SS24 SS27 SS3 SS5 SS8 W.TXT

17- Write a Shell Script that examines each file in the current directory. Files whose namesend in old are moved to a directory named old files and files whose names end in .c aremoved to directory named cprograms.

echo Before "\n"
ls -l
mkdir oldfiles cprograms
for var in `ls`
do
if test $var = *old
then
echo "\n" File $var is moved to old files directory
mv $var old files
fi
if test $var = *.c
then
echo"\n" File $var is moved to cprograms directory
mv $var cprograms
fi
done
cd oldfiles
echo "\n" Files in oldfiles
ls -l
cd ..
echo "\n" After"\n"
ls -l

Output:

sh SS18
Before
total 144-rwxrwxrwx 1 ashish ashish 66 2008-11-20 10:07 COLD
-rwxrwxrwx 1 ashish ashish 0 2008-11-20 10:07 E.TXT
-rwxrwxrwx 1 ashish ashish 7 2008-11-20 10:07 F1
-rwxrwxrwx 1 ashish ashish 54 2008-11-20 10:07 GOLD
Files in oldfiles
total 0
After
total 152
-rwxrwxrwx 1 ashish ashish 66 2008-11-20 10:07 COLD
drwxr-xr-x 2 ashish ashish 4096 2008-11-20 12:04 cprograms
-rwxrwxrwx 1 ashish ashish 0 2008-11-20 10:07 E.TXT
-rwxrwxrwx 1 ashish ashish 7 2008-11-20 10:07 F1
-rwxrwxrwx 1 ashish ashish 54 2008-11-20 10:07 GOLD

18- Write a Shell Script which searches all files in the given directory (to be taken as command line argument) for the file having the title (to be taken as command line argument), as the first line in the file.

a) Display the contents of the searched file.
b) In the end, printthe the file is ###, where

### is small-sized if total no. of lines is <50 data-blogger-escaped-span="span">
### is medium-sized if total no. of lines between 50&100
### is large-sized.
for i in `ls $dirnm`
do
f=`echo $1/$i`
if [ -f $f ]
then
if [ `cat $f|head -1` = $2 ]
then
cat $f
if [ `cat $f|wc -l` -lt 50 ]
then
echo $i is small sized
fi
if [ `cat $f|wc -l` -ge 50 -a `cat $f|wc -l` -lt 100 ]
then
echo $i is a medium sized
fi
if [ `cat $f|wc -l` -gt 100 ]
then
echo $i is large sized
fi
fi
fi
done

Output:

sh ss19 newdir AMITY
AMITY
Amity University
file1 is small sized

19- Write a shell script which reports names and sizes of all files in a directory (directory would be supplied as an argument to the shell script) whose size is exceeding 1000 bytes.The file names should be printed in descending order of their sizes. The total number of such files should also be reported.

Cd $1
mkdir
tmp$1
for i in *
do
if [ -f $i ]
then
tmp=`ls -l $i|cut -f5 -d" "`
if [ $tmp -gt 1000 ]
then
ln $i tmp$1/$i
fi
fi
done
ls -lS tmp$1
echo Total number of such files is : `ls tmp$1|wc -w`
rm -r tmp$1

output:

sh SS20
total 4-rw-r--r-- 2 ashish ashish 1392 2008-11-20 11:02 unix output
Total number of such files is : 1

20- WASS for renaming each file in the directory such that it will have the current shell PID as an extension. The shell script should ensure that the directories do not get renamed.

for var in `ls`
do
if test -f $var
then
a=`echo $$`
mv $var $var.$a
fi
done
echo "\n" File name changed:"\n"
ls -l

Output:

sh SS21.7600.a
File name changed:
total 152
-rwxrwxrwx ashish ashish 66 2008-11-20 10:07 COLD.7600.a.a
drwxr-xr-x 2 ashish ashish 4096 2008-11-20 12:04 cprograms
-rwxrwxrwx 1 ashish ashish 0 2008-11-20 10:07 E.TXT.7600.a.a
-rwxrwxrwx 1 ashish ashish 7 2008-11-20 10:07 F1.7600.a.a
-rwxrwxrwx 1 ashish ashish54 2008-11-20 10:07 GOLD.7600.a.a
drwxr-xr-x 2 ashish ashish 4096 2008-11-20 12:04 oldfiles


21- WAP to calculate and print the first m Fibonacci numbers.

echo Enter the series length
read num
x=0
y=1
if test $num -eq 1
then echo $x
else if test $num -eq 2
then echo "$x\n$y"
else
echo "$x\n$y"
i=3
while test $i -le $num
do
temp=`expr $y + $x`
x=$y
y=$temp
echo $y
i=`expr $i + 1`
done
fi
fi

Output:

sh SS22
Enter the series length
6
0
1
1
2
3

22- WASS that will receive any number of file names as arguments. The shell script should check whether such files already exist. If they do, then it should be reported. The files that do not exist should be created in a sub-directory called my dir. The shell script should first check whether the sub-directory my dir exists in the current directory. If it doesn’t exist,then it should be created. If my dir already exists, then it should be reported along with the number of files that are currently present in my dir.

if [ -e mydir ]
then
echo "Directory : mydir exist"
else
echo Do not exist
mkdir mydir
fi
for a in $*
do
echo $a
then
echo "File does not exist "
else
echo “file d
touch $a
mv $a mydir
fi
done

Output:

sh SS23 SS22
Directory : mydir exist
SS22
File does not exists

23-  A shell script receives even number of file names. Suppose four file names are supplied,then the first file should get copied into second file, the third file should get copied into fourth and so on. If odd number of file names is supplied then no copying should take place and an error message should be displayed.

if [ `expr $# % 2` -ne 0 ]
thenecho Enter even number of parameters
else
i=0
for k in $*
do
i=`expr $i + 1`
if [ $i -eq 1 ]
then
temp1=$k
fi
if [ $i -eq 2 ]
then
temp2=$k
i=0
cp $temp1 $temp2
fi
done
fi
cat

Output:

$ cat>txt
File Ashish lives in Delhi$ cat>doc
Ashish is a student of MCA$ cat>tree
Ashish is a good student$ cat>wee
His roll no- is A1004807024
$ sh SS24 txt doc tree wee
His roll no- is A1004807024 Ashish is good student Ashish lives id Delhi Ashish is a student of BCA


24- WASS to identify all zero-byte files in the current directory and delete them. Before proceeding with deletion, the shell script should get a conformation from the user.

for i in *
do
if [ -e $i -a -f $i ]
then
if [ -s $i ]
then
echo
else
rm -i $i
fi
fi
done

Output:

sh SS25
rm: remove regular empty file `E.TXT'?  y
rm: remove regular empty file `Q.TXT'?  n
rm: remove regular empty file `W.TXT'? n

25- WASS to compute the GCD and LCM of two numbers.

echo Enter First number
read n1
echo Enter Second number
read n2
if [ $n1 -lt $n2 ]
then
i=$n1
else
i=$n2
fi
flag=0
while [ $flag -eq 0 ]
do
if [ `expr $n1 % $i` -eq 0 -a `expr $n2 % $i` -eq 0 ]
then
echo GCD of $n1 and $n2 is $i
flag=1
fi
i=`expr $i – 1`
done

Output:

sh SS26
Enter First number
4
Enter Second number
8
GCD of 4 and 8 is 4

26- Two numbers are entered through the keyboard. WAP to find the value of one number raised to the power of another.

read b
echo Enter power
read p
powr=$p
result=1
while [ $p -ge 1 ]
do
result=`expr $result \* $b`
p=`expr $p – 1`
done
echo $b raised to the power $powr is $result

Output:

sh SS27
Enter a number
4
Enter power
2
4 raised to the power 2 is 16

27- WASS that prompts the user for the password. The user has maximum of 3 attempts. If the user enters the correct password, the message “Correct Password” is displayed else the message “Wrong Password”.

echo Set the password first
read passw
i=0
while [ $i -lt 3 ]
do
echo Enter password
read p
if [ $p = $passw ]
then
echo Correct Password
i=3
else
echo Wrong password
i=`expr $i + 1`
fi
done

Output:

sh SS28
Set the password first
ashish
Enter password
ashish
Correct Password

28-  WASS that repeatedly asks the user repeatedly for the “Name of the Institution” until the user gives the correct answer.

Flag=0
while [ $flag -eq 0 ]
do
echo Enter name of your institute in capital letters
read inst
if [ $inst = "AMITY" ]
then
echo Correct Answer
flag=1
else
echo Enter Again
fi
done

output:

sh SS29
Enter name of your institute in capital letters
AMITY
Correct Answer

29-  WAP to generate all combinations of 1, 2 and 3 using for loop.

for i in 1 2 3
do
for j in 1 2 3
do
for k in 1 2 3
do
echo $i$j$k
done
done
done

Output:

sh SS30
111
112
113
121
122
123
131
132
133
211
212
213
221
222
223
231
232
233
311
312
313
321
322
323
331
332
333

1 comments:

  1. Hi, i feel that i saw you visited my web site thus i came to go back the prefer?
    .I am attempting to in finding issues to enhance my site!

    I suppose its ok to use a few of your concepts!
    !

    Look at my web site :: free trial anti virus
    Also see my webpage > ebooks to sell

    ReplyDelete