MCA 2nd sem unix lab programs

1.a. A shell script for non-recursive, which accepts any number of arguments and prints them in the reverse order(for example, if script is named rags, then executing rags A B C should produce C B A on the standard output).

if [ $# -eq 0 ] ; then
echo “argument not found”
exit
else
i=`echo $*|wc -w`
while [ $i -gt 0 ] ; do
s=`echo $*|cut -d ” ” -f $i`
temp=`echo $temp $s`
i=`expr $i – 1`
done
echo “Reversed list : $temp”
fi

OUTPUT:

sh 1a.sh a b c
Reversed list : c b a

1.b. A shell script that accepts two file name as arguments, checks if the permission for these files are identical and if the permissions are identical, output common permissions otherwise output each file name followed by its permissions.

if [ $# -eq 0 ] ; then
echo “No arguments”
exit
elif [ $# -lt 2 ] ; then
echo “Only one arguments”
exit
else
f1=`ls -l $1|cut -c ’2-10′`
f2=`ls -l $2|cut -c ’2-10′`
if [ "$f1" == "$f2" ] ; then
echo “File permission are identical”
else
echo “File permission are not identical”
echo “The permission of first file is f1:$f1″
echo “The permission of the second file is f2:$f2″
fi
fi

OUTPUT:

sh 1b.sh 1a.sh bhu.sh
File permission are not identical
The permission of first file is f1:rw-rw-r–
The permission of the second file is f2:rwxrw-r–


2.a. A shell script that takes a valid directory name as an argument and recursively descend all the sub directories, finds the maximum length of any file in that hierarchy and writes this maximum value to the standard output.

echo “Enter Directory name”
read dir
if [ ! -d $dir ] ; then
echo “Invalid directory”
exit
fi
large=0
for file in `find $dir -type f`
do
size=`stat -c %s $file`
echo “size of $file is $large”
if [ $size -gt $large ] ; then
large=$size
fi
done
echo ” File with Maximum size is $large”

OUTPUT:

sh 2a.sh
Enter Directory name
singh
size of singh/1b.sh is 593
size of singh/bhupal is 618
size of singh/bhu.sh is 618
size of singh/1a.sh is 618
File with Maximum size is 618

2.b. A shell script that accepts a path name and creates all the components in that path name as directories. For example, if the script is named mpc, than the command mpc a/b/c/d should create directories a, a/b, a/b/c, a/b/c/d.

temp=$IFS
IFS=/
for i in $*
do
if [ -d $i ] ; then
cd $i
else
mkdir $i
cd $i
fi
done
IFS=$temp

OUTPUT:

sh 2.sh a b c d
cd a
ls
b
cd b
ls
c
cd c
ls
d


3.a. A shell script that accepts valid login-in names as arguments and prints there corresponding home directories, if no arguments are specified, print a suitable error message.

if [ $# -eq 0 ] ; then
echo “No arguments”
else
for name in $*
do
if grep $name /etc/passwd >/dev/null
then
echo “Login name:$name”
hdir=`grep $name /etc/passwd | cut -d”:” -f6`
echo “Home Directory :$hdir”
else
echo “$name is not valid Login name”
fi
done
fi

OUTPUT:

sh 3a.sh
No arguments

sh 3a.sh abc
abc is not valid Login name

sh 3a.sh root
Login name:root
Home Directory :/home/root


3.b. A shell script to implement terminal locking (similar to the lock command). It should prompt the user for a password. After accepting the password entered by the user, it must prompt again for the matching password as confirmation and if match occurs, it must lock the keyboard until a matching password is entered again by the user, note that the script must be return to disregard BREAK, control –D. no time limit need be implemented for the lock duration.

stty -echo
echo “Enter password”
read pass1
echo “Confirm password”
read pass2
if [ "$pass1" = "$pass2" ] ; then
echo “Terminal is locked”
trap ”1 2 15
while true ; do
echo ” Enter password”
read pass3
if [ "$pass3" = "$pass2" ]; then
echo “Terminal Unlocked”
stty echo
exit
else
echo “Try again”
fi
done
else
echo “password do not match”
stty echo
fi

OUTPUT:
sh 3b.sh
Enter password
Confirm password
Terminal is locked
Enter password
Try again
Enter password
Terminal Unlocked

4.a. Creating a script file called file-properties that reads a file name entered and outputs it properties.

echo “Enter a file name”
read file
if [ ! -e $file ] ; then
echo “File does not exit”
exit
else
ftype=`ls -l $file|cut -c 1`
fper=`ls -l $file|cut -c 2-10`
fowner=`ls -l $file|tr -s ‘ ‘|cut -d ” ” -f3`
fsize=`ls -l $file|tr -s ‘ ‘|cut -d ” ” -f5`
fdate=`ls -l $file|tr -s ‘ ‘|cut -d ” ” -f6`
ftime=`ls -l $file|tr -s ‘ ‘|cut -d ” ” -f7`
fname=`ls -l $file|tr -s ‘ ‘|cut -d ” ” -f8`

fi
echo “The file type is : $ftype”
echo “The file permission is : $fper”
echo “The file owner is : $fowner”
echo “The file size is : $fsize”
echo “The file date is : $fdate”
echo “The file time is : $ftime”
echo “The file name is : $fname”

OUTPUT:

sh 4a.sh
Enter a file name
2.sh
The file type is : -
The file permission is : rw-rw-r–
The file owner is : root
The file size is : 104
The file date is : 2009-04-30
The file time is : 02:31
The file name is : 2.sh

4.b. A shell script that accept one or more file names as arguments and convert all of them to upper case, provided they exist in current directory.

if [ $# -eq 0 ] ; then
echo “No Arguments”
exit
else
for file in $*
do
if [ -e $file ] ; then
fname=`echo $file|tr ‘[a-z]‘ ‘[A-Z]‘`
echo “The $file is converted to $fname “
else
echo “The directory $file does not exit”
fi
done
fi

OUTPUT:

sh 4b.sh dirname
The bhupal is converted to DIRNAME

sh 4b.sh abc
The directory abc does not exit

5.a. A shell script that displays all the links to a file specified as the first argument to the script. The second argument , which is optional, can be used to specify in which the search is to begin, if this second argument is not present, the search is to begin in current working directory. In either case, the starting directory as well as all its subdirectories at all levels must be searched. The script need not include any error checking.

if [ $# -eq 0 ] ; then
echo “No arguments”
exit
fi
if [ $# -eq 2 ] ; then
dir=$2
else
dir=`pwd`
fi
inode=`stat -c %i $1`
count=0
for link in `find $dir -inum $inode`
do
echo $link
count=`expr $count + 1`
done
if [ $count -eq 0 ] ; then
echo “$1 has no link in the directory $dir”
else
echo “$1 has $count links in the directory $dir”
fi

OUTPUT:

sh 5a.sh hi
/home/root/hi
hi has 2 links in the directory /home/root

5.b. A shell script that accepts as file-name as argument and display its creation time if file exist and if it does not send output error message.

if [ $# -eq 0 ] ; then
echo “No Arguments”
else
for file in $*
do
if [ -f $file ] ; then
fdate=`ls -l $file|tr -s ‘ ‘|cut -d ” ” -f6`
ftime=`ls -l $file|tr -s ‘ ‘|cut -d ” ” -f7`
else
echo “$file does not exit”
fi
done
fi
echo “File modified on $fdate at $ftime”

OUTPUT:

sh 5b.sh 2.sh
File modified on 2009-04-30 at 02:31

6.a. A shell script to display the calendar for the current month with current date
Replace by * or ** depending on whether the date has one digit or two digits.

dt=`date +%d`
cal > f3.lst
if [ $dt -lt 10 ] ; then
dt=`echo $dt|cut -c 2`
ln=`sed -n ’3,$p’ f3.lst | nl | grep “$dt” | head -1 | cut -f1`
ln=`expr $ln + 2`
sed $ln’s/’$dt’/*/’ f3.lst
else
sed ‘s/’$dt’/**/’ f3.lst
fi

OUTPUT:

sh 6a.sh
May 2009
Su Mo Tu We Th Fr Sa
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 **
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31

sh 6a.sh
May 2009
Su Mo Tu We Th Fr Sa
* 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

6.b. A shell script to find smallest of three numbers that are read from keyboard.

echo “Enter the three numbers”
read a b c
if [ $a -lt $b -a $a -lt $c ] ; then
echo “$a is sma llest”
elif [ $b -lt $c ] ; then
echo “$b is smallest”
else
echo “$c is smallest”
fi

OUTPUT:

sh 6b.sh
Enter the three numbers
5 12 3
3 is smallest

sh 6b.sh
Enter the three numbers
1 2 3
1 is sma llest

sh 6b.sh
Enter the three numbers
10 6 20
6 is smallest

7.a. A shell script using expr command to read in a string and display a suitable message if it does not have at least 10 characters.

echo “Enter the string”
read str
if [ -z str ] ; then
echo “null character”
else
len=`expr “$str” : ‘.*’`
if [ $len -ge 10 ] ; then
echo “$str has $len character”
else
echo “$str has less than 10 character”
fi
fi

OUTPUT:

sh 7a.sh
Enter the string
nittemeenakshi
nittemeenakshi has 14 character

sh 7a.sh
Enter the string
hello
hello has less than 10 character

7.b. A shell script to compute the sum of number passed to it has argument on command line and display the result.

echo “To compute the sum”
num=$1
sum=0
while [ $num -gt 0 ] ; do
rem=`expr $num % 10`
num=`expr $num / 10`
sum=`expr $rem + $sum`
done
echo “The result is $sum”

OUTPUT:

sh 7b.sh 245
To compute the sum
The result is 11

sh 7b.sh 123
To compute the sum
The result is 6

8.a. A shell script that compute gross-salary for an employee, according to the rule given below.
If basic salary is = 15000 than HRA=500 of basic and DA=98% of basic.

echo “Enter the basic salary”
read basic
if [ $basic -lt 15000 ] ; then
hra=`expr $basic \* 10 / 100`
da=`expr $basic \* 90 / 100`
else
hra=`expr $basic \* 50 / 100`
da=`expr $basic \* 98 / 100`
fi
gross=`expr $basic + $hra + $da`
echo “Gross salary is $gross”

OUTPUT:

sh 8a.sh
Enter the basic salary
23456
Gross salary is 58170

sh 8a.sh
Enter the basic salary
1234
Gross salary is 2467

8.b. A shell script that delete all lines containing a specific word in one or more file supplied as argument to it.

if [ $# -eq 0 ] ; then
echo “No arguments”
else
pattern=$1
shift
for fname in $*
do
if [ -f $fname ] ; then
echo “Deleting $pattern from $file”
sed ‘/’$pattern’/d’ $fname
else
echo “$fname not found”
fi
done
fi

OUTPUT:

cat > hello
hi this is singh here
studying in nmit college

sh 8b.sh singh hello
Deleting singh from
studying in nmit college


9.a. A shell script that gets executed displace the message either “Good Morning ” or “Good Afternoon” or “Good Evening” depending upon time at which the user logs-in.

time=`who am I | tr -s ‘ ‘ | cut -d ” ” -f 4 | cut -c 1,2`
if [ $time -le 12 ] ; then
echo “Good Morning $LOGNAME”
elif [ $time -gt 12 -a $time -lt 16 ] ; then
echo “Good Afternoon $LOGNAME”
else
echo “Good Evenini $LOGNAME”
fi

OUTPUT:

sh 9a.sh
Good Morning root

sh 9a.sh
Good Afternoon root

9.b. A shell script that accept a list of file names as its arguments, count and report occurrence of each word that is present in the first argument file on other argument files.

if [ $# -eq 0 ] ; then
echo “No Arguments”
elif [ $# -eq 1 ] ; then
echo “Only one Arguments”
else
pat=$1
if [ ! -e $1 ] ; then
echo “$1 does not exist”
exit
fi
shift
for file in $*
do
if [ -e $fie ] ; then
echo $file
for pattern in `cat $pat`
do
echo “$pattern occurs `grep -c “$pattern” $file` times”
done
else
echo “$file does not exist”
fi
done
fi

OUTPUT:
sh 9b.sh hello hi
hi
hi occurs 1 times
this occurs 1 times
is occurs 1 times
singh occurs 1 times
here occurs 0 times
studying occurs 1 times
in occurs 2 times
nmit occurs 0 times

if [ $# -eq 0 ] ; then
echo “No Arguments”
exit
fi
if who | grep “$1″ ; then
uhtime=`who | grep $1 tr -s ‘ ‘ | cut -d ” ” -f4 | cut -c 1,2`
umtime=`who | grep $1 tr -s ‘ ‘ | cut -d ” ” -f4 | cut -c 4,5`
hh=`$uhtime | cut -d ” ” -f1`
mm=`$umtime | cut -d ” ” -f1`
shtime=`date +%H`
smtime=`date +%M`
timeh=`expr $shtime – $hh`
timem=`expr $smtime – $mm`

echo “System time is $shtime hr and $smtime min”
echo “Time in which is working $timeh hr and $timem min”
else
echo “No Valid login name”
fi

10.b. A shell script that reports the logging in of a specified user with in one minute after he/she login. The script automatically terminates if specified user does not login during a specified period of time.

interval=5
name=$1
who | awk ‘{printf $1}’ | grep “$name” > /dev/null
if test $? = 0 ; then
loggedin=true
echo “$name logged in”
else
loggedin=false
echo “$name not logged in”
fi
sleep $interval
while true ; do
who | awk ‘{printf $1}’ | grep “$name” > /dev/null
if test $? = 0 ; then
if loggedin=false ; then
loggedin=true
echo “$name logged in”
exit
fi
else
if loggedin=true ; then
loggedin=false
echo “$name not logged in”
exit
fi
fi
done

OUTPUT:

sh 10b.sh root
root logged in

root logged in


11.a. A shell script that accepts two integers as its arguments and compute the value of first number raised to the power of second number.

x=$1
y=$2
z=$x
i=1
if [ $# -eq 0 ] ; then
echo “No Arguments”
else
while [ $i -lt $y ] ; do
z=`expr $z \* $x`
i=`expr $i + 1`
done
echo “Value of 1st number raised to the power of 2nd number : $z”
fi

OUTPUT:

sh 11a.sh 3 4
Value of 1st number raised to the power of 2nd number : 81

11.b. A shell script that accept the file name, starting and ending line number as an argument and display all the lines between the given line number.

if [ $# -eq 0 ] ; then
echo “No argument”
elif [ $# -eq 1 ] ; then
echo “Only one argument”
elif [ $# -eq 2 ] ; then
echo “only two argument”
else
if [ ! -e $1 ] ; then
echo “File does not exist”
else
sed -n ”$2′,’$3′p’ $1
fi
fi

OUTPUT:

sh 11b.sh 11a.sh 3 6
z=$x
i=1
if [ $# -eq 0 ] ; then
echo “No Arguments”

sh 11b.sh 11a.sh 3 5
z=$x
i=1
if [ $# -eq 0 ] ; then

12.aWrite a shell script that folds long lines into 40 columns. Thus any line that exceeds 40 characters must be broken after 40th, a “\” is to be appended as the indication of folding and the processing is to be continued with the residue. The input is to be supplied through a text file created by the user
BEGIN {
printf”Begin of a file”;
}
{
x1=length($0);
if(x1 text.txt

after tracing the process of establishment and growth of operational research
we consider it has source of other new science literally the
word operation may be defined as some action that we applied to some problem.

awk -f 12a.awk text.txt
Begin of a file

after tracing the process of establishme \
nt and growth of operational research
we consider it has source of other new s \
cience literally the
word operation may be defined as some ac \
tion that we applied to some problem.End of file


12.b Write an awk script that accepts date argument in the form of mm-dd-yy and displays it in the form if day, month, and year. The script should check the validity of the argument and in case of error, display a suitable message.

clear
BEGIN { printf “date validation” }

{
if ( ( $1==2 && $2>29 ) || ( $2 > 31 ) || ( $1==4 || $1==6 || $1==9 || $1==11 && $2>30 )
|| ( $3%4!=0 && $1==2 && $2>28 )||($1 > 12) )
printf “invalid date”
else
printf “\n%d:%d:%d\n”,$2,$1,$3
}
END {“date conversion”}

OUTPUT:

awk -f san12b.awk
date validation14 17 09
invalid date

awk -f san12b.awk
date validation12 18 09

18:12:9


13.a Write an awk script to delete duplicated line from a text file. The order of the original lines must remain unchanged.

clear
BEGIN { printf “script to delete duplicate line ” }
{ a[n++]=$0 }
END {
for(i=0;i<n;i++)
{
flag=0
for(j=0;j aaa
bhu
singh
anand
bhu

awk -f san13a.awk aaa
script to delete duplicate line bhu

13 .b .Write an awk script to find out total number of books sold in each discipline as well as total book sold using associate array down table as given below.
i. Electrical 34 ii. Mechanical 67 iii. Electrical 80
iv. Computer Science 43 v. Mechanical 65 vi. Civil 198
vii. Computer Science 64

BEGIN { printf “total no. of book sold in each category” }
{ books[$1]+=$2 }
END { for(item in books)
{ printf(“\n\t%-17s%1s%-5d”,item,”=”,books[item])
total+=books[item]
}
printf(“%-17s%1s%-5d\n”,”total books sold”,”=”,total)
}

OUTPUT:

cat >book.txt
mechanical 67
electrical 34
electrical 80
computers 43
mechanical 65
civil 198
computers 64

awk -f san13b.awk book.txt
total no. of book sold in each category
computer =107
electrical =114
civil =198
mechanical =132 total books sold =551

14. Write an awk script to compute gross salary of an employee accordingly to the rule given below:
If basic salary = 10,000 then HRA=20% of basic and DA=50% of basic.

BEGIN { printf “enter the basic pay: Rs”
getline bp <"/dev/tty"
if(bp<10000)
{
hra=.15*bp
da=.45*bp
}
else
{
hra=.2*bp
da=.5*bp
}
gs=bp + hra + da
printf "gross salary = Rs. %.2f\n",gs
}

OUTPUT:

awk -f san14.awk
enter the basic pay: Rs7000
gross salary = Rs. 11200.00

awk -f san14.awk
enter the basic pay: Rs12000
gross salary = Rs. 20400.00

About these ads

10 Responses

  1. Thanks a lot for uploading. It helped me a lot.. Thank U so much :)

  2. Thanks a lot sir ..
    your work deserves for acknowledgement and appreciation …….this is really helpful for me

  3. We are very thank full to you. You made very good job. All programs very use full to every one…….

  4. some programs are not worked

  5. thanks ….it is useful..

  6. Thank you very much man..
    Its too good..
    Its a joomla based site right?

  7. Thanks a lot SD :D

  8. thank you.. wish you all the best..

  9. Thank you… for best program dig it your self.. all programs are working fine.. if its not working in your system its your problem not mine..

    Enjoy tc..
    Guru

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.