0% found this document useful (0 votes)
58 views

Unix Lab Programs

The document contains code snippets for various UNIX programs and utilities. Some of the programs allow the user to view file permissions, owner, size and other details. Other programs perform tasks like locking the terminal, finding hard and soft links of a file, formatting calendars, copying files, filtering output based on line lengths, and calculating salary details using awk.

Uploaded by

Akshaysagar.g
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
58 views

Unix Lab Programs

The document contains code snippets for various UNIX programs and utilities. Some of the programs allow the user to view file permissions, owner, size and other details. Other programs perform tasks like locking the terminal, finding hard and soft links of a file, formatting calendars, copying files, filtering output based on line lengths, and calculating salary details using awk.

Uploaded by

Akshaysagar.g
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

UNIX LAB INTERNALS-2 PROGRAMS

(3a.)
i=1
while [ $i -eq 1 ]
do
echo " 1.File permission \n 2.Link information\n 3.Owner name\n 4.Group
info\n 5.file size\n
6.Creation\n 7.File name"
echo "enter the choice"
read ch
case $ch in
1 )echo `ls -l $1 |cut -d" " -f1` ;;
2 )echo `ls -l $1 |cut -d" " -f2` ;;
3 )echo `ls -l $1 |cut -d" " -f3` ;;
4 )echo `ls -l $1 |cut -d" " -f4` ;;
5 )echo `ls -l $1 |cut -d" " -f5` ;;
6 )echo `ls -l $1 |tr -s" "|cut -d" " -f6-8` ;;
7 )echo `ls -l $1 |tr -s" "|cut -d" " -f9` ;;
* )echo "invalid choice" ;;
esac
echo "do you want to continue yes=1 no=0"
read i
done

output
student@mca-desktop:~$ sh 3a.sh f1
1.File permission
2.Link information
3.Owner name
4.Group info
5.file size
6.Creation
7.File name
enter the choice
1
-rwxrwxrwx
do you want to continue yes=1 no=0
(3b)
trap " " 1 2 3 5 12 24
stty -echo
echo "enter password to lock the terminal"
read p1
echo "enter the password for confirmation to lock"
read p2
stty -echo
if [ $p1 = $p2 ]
then
echo "terminal locked sucessfully"
echo "to unlock enter password"
read p3
until [ $p3 = $p2 ]
do
echo "enter right password to write password to unlock"
read p3
done
stty echo
else
echo "password not matched can't lock your terminal"
stty echo
fi

(4.b)
num=`ls -li $1|cut -d " " -f1`
echo "$num\n"
if [ $# -eq 1 ]
then
echo "hard links are :"
find . -inum $num -print
echo "soft links are:"
find . -lname $1 -print
else
echo "hard links are :"
find $2 -inum $num -print
echo "soft links are:"
find $2 -lname $1 -print
fi
output
student@mca-desktop:~$ sh 4b.sh f1
hard links are:
./d1/somef
./d1/f6
./f1
860730
soft links are:
./d1/soft

(5b)
d=`date | cut -d" " -f3`
echo $d
if [ $d -lt 10 ]
then
sed s/" $d "/" * "/ ppp
else
sed s/" $d "/" ** "/ ppp
fi
if [ ! -e ppp ]
then
cal>ppp
fi

output
student@mca-desktop:~$ sh 5b1.sh
13
November 2014
Su Mo Tu We Th Fr Sa
1
2 3 4 5 6 7 8
9 10 11 12 ** 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30
(6a)
if [ -d mydir ]
then
echo "mkdir already existing"
else
mkdir mydir
fi
for fn in $*
do
if [ -e $fn ]
then
cp $fn mkdir
echo "$fn copied sucessfully to mydir"
else
echo "$fn can not copy its not existing"
fi
done

output:
student@mca-desktop:~$ cat>f1
hello

student@mca-desktop:~$ cat>f2
welcome to unix lab

student@mca-desktop:~$ sh 6a.sh f1 f2 f3 f4 f5
mydir1 already existing
f1 copied successfully to mydir1
f2 copied successfully to mydir1
f3 can't copy its not existing
f4 can't copy its not existing
f5 can't copy its not existing
(6b)
ls -lr | grep "^-" | tr -s " " | cut -d " " -f9>f1
for filename in `cat f1`
do
ch=`expr length $filename`
if [ $ch -ge 10 ]
then
echo filename = $filename ch=$ch
fi
done

output
student@mca-desktop:~$ sh 6b1.sh f1

filename = Proxy05.java ch=12


filename = Proxy05.class ch=13
filename = prog1.java~ ch=11
filename = prog1.java ch=10
filename = prog1.class ch=11
filename = prog11.java~ ch=12
filename = prog11.java ch=11
filename = prog11.class ch=12

(7a)
time=`date + "%H"`
echo $time
case $time in
[0][0-9]|[1][0-1]) echo " good morning "; ;
[1][2-5] ) echo " good afternoon " ; ;
[1][6-9] ) echo " good evening " ; ;
* ) echo " good night "; ;
esac
(9a)
l=`cat $1|wc -l`
if [ $2 -lt $3 -a $3 -le $l ]
then
i=`expr $3 - 1`
j=`expr $i - $2`
head -$i $1 | tail -$j
else
echo "invalid starting and ending line number"
fi

(9b)
tl=`cat $1|wc -l`
i=1
while [ $i -le $tl ]
do
line=`cat $1|head -$i|tail -1`
x=`expr length "$line"`
a=1
b=40
while [ $b -lt $x ]
do
p=`echo $line|cut -c$a-$b`
echo " $p\ "
a=`expr $b + 1`
b=`expr $b + 40`
done
echo $line|cut -c$a-$x
i=`expr $i + 1`
done
(11.a)
{
sum+=$2
a[$1]=a[$1]+$2
}
END{
for(i in a)
{
print i"==>"a[i]
}
print "total books sold="sum
}

(11b)
{
print"enter basic"
getline basic
if(basic<10000)
{
hra=0.45*basic
da=0.45*basic
}
else
{
hra=0.20*basic
da=0.50*basic
}
gross=basic+da+hra
print "basic="basic"\tda="da"\thra="hra"\tgross="gross
}
output:
student@mca-desktop:~$ awk -f 11b.awk

enter basic
9999
basic=9999 da=4499.55 hra=1499.85 gross=15998.4

enter basic
10000
basic=10000 da=5000 hra=2000 gross=17000

enter basic
15000
basic=15000 da=7500 hra=3000 gross=25500

enter basic
15500
basic=15500 da=7750 hra=3100 gross=26350

You might also like