Académique Documents
Professionnel Documents
Culture Documents
LAB MANUAL
(MCA- 13MCA17)
Prepared by
clear
if [ $# -ne 1 ]
then
echo “Invalid number of arguments passed”
exit
fi
if [ ! -d $1 ]
then
echo “Directory does not exist”
ms=`ls -lR $1 | grep ‘^-‘ | tr –s “ “ | cut -d “ “ -f5,9 | sort -n | tail -1`
echo “The max file size is $ms”
OUTPUT
if [ $# -ne 1 ]
then
echo “Invalid number of arguments”
else
mkdir -p $1
fi
OUTPUT
$sh 2b.sh
Invalid number of arguments
clear
if [ $# -ne 2 ]
then
echo “Invalid number of arguments”
else
ls -l $1 | cut -d “ “ -f1 > file1
ls -l $2 | cut -d “ “ -f1 > file2
if cmp file1 file2
then
echo -e”\n\nBoth files have same permissions\n”
cat file1
else
echo -e “\n\nFiles have different permissions\n”
echo “The permissions of file $1”
cat file1
echo “The permissions of file $2”
cat file2
fi
fi
OUPUT
clear
if [ $# -eq 0 ]
then
echo “No command line arguments passed”
exit
fi
while [ $1 ]
do
cat /etc/passwd | cut -d “:”-f1 | grep “^$1” > temp
ck=`cat temp`
if [ “$ck” ! = “$1” ]
then
echo “Error:$1 is an invalid log-in name”
else
echo “Home Directory for $1 is:”
cat /etc/passwd | grep “$1” | cut -d “:” -f6
fi
shift
done
OUTPUT
clear
echo -e “Enter the filename:\c”
read fn
if [ -f $fn ]
then
echo “File permissions are”
echo `ls -l $fn | cut -d “ “ -f1`
echo “Number of links to the file”
echo `ls -l $fn | cut -d “ “ -f2`
echo “File size”
echo `ls -l $fn | cut -d “ “ -f5`
echo “Last modified date”
echo `ls -l $fn | cut -d “ “ -f6`
else
echo “File not found”
fi
OUTPUT
$sh 3a.sh
Enter the filename:test
File permissions are
-rw-rw-r--
Number of links to the file
3
File size
48
Last modified date
Oct 9
3. b. Write 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 keyword until a matching password is entered again by the
clear
trap “ “ 2 20
stty -echo
echo -e “Enter the password:\c”
read key1
echo -e “Re-enter the password:\c”
read key2
if [ “$key1” = “$key2” ]
then
until [ “$key3” = “$key2” ]
do
echo -e “Enter correct password to unlock:\c”
read key3
done
else
echo “Mismatched password”
fi
stty echo
OUTPUT
$sh 3b.sh
Enter the password:
Re-enter the password:
Enter correct password to unlock:
Enter correct password to unlock:
4. a. Write a shell script that accept one or more filenames as argument and convert all of
them to uppercase, provided they exist in current directory.
clear
if [ $# -eq 0 ]
then
OUTPUT
4. b. Write 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 [ “$2” != “ “ ]
OUTPUT
5. a. Write a shell script that accepts as filename as argument and display its creation time
if file exist and if it does not send output error message.
clear
if [ $# -ne 1 ]
then
echo “Invalid number of arguments”
exit
fi
if [ -e $1 ]
OUTPUT
5. b. Write a shell script to display the calendar for current month with current date
replaced by * or ** depending on whether the date has one digit or two digits.
#!bin/bash
a=`date +%e`
if [ $a -lt 10 ]
tehn
cal|sed “s/ $a/ */”
else
cal | sed “s/$a/**/”
fi
$sh 5b.sh
April 2008
M T W T F S S
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 ** 19 20 21 22 23
24 25 26 27 28 29 30
31
6a. Write a shell script to find a file/s that matches a pattern given as command line
argument in the home directory, display the contents of the file and copy the file into
the directory ~/mydir
clear
if [ $# -eq 0 -o $# -gt 1 ]
then
echo "invalid files "
exit
else if [ -d mydir ]
then
cd mydir
rm *
cd ..
contents
1 Sudarsanam
2 Anil kumar
3 Lakshmi
4 Vasuki
5 Prhvi
6 Charan
7 VTU
8 Beguam
clear
ls >listfiles
echo -e "the list of files greater than a\n"
for i in `cat listfiles`
do
len=`expr length "$i"`
if [ $len -gt 9 ]
then
echo $i
fi
done
OUTPUT
$sh 6b.sh
the list of files greater than 9
example1.sh
example2.sh
example3.sh
example4.sh
clear
h=`who | head -1 | tr -s “ “ | cut -d “ “ -f4 | cut -d “:” -f1`
if [ $h -lt 12 ]
then
echo “Good Morning”
elif [ $h -ge 12 -a $h -lt 17 ]
then
echo “Good Afternoon”
else
echo “Good Evening”
fi
OUTPUT
$sh 7a.sh
Good Afternoon
OUTPUT
cat f1 cat f2
suda suda
mca suda
deepthi mca
charan mca
$sh 7b.sh f1 f2
count Word=suda in f2
2
count Word=mca in f2
2
count Word=charan in f2
0
OUTPUT
$sh 8a.sh
Enter the Username : mca05
Username : mca05
Login period :00:17
OUTPUT
$sh 8b.sh
Enter the username :mca05
Username :mca05 has logged in
clear
if [ $# -ne 3 ]
then
echo "Invalid number of arguments"
exit
fi
c=`cat $1 | wc -l`
if [ $2 -le 0 -o $3 -le 0 -o $2 -gt $3 -o $3 -gt $c ]
then
echo "Invalid Input"
exit
fi
sed -n "$2, $3 p" $1
Invalid number of arguments
cat 9a.txt
1 Sudarsanam
2 Anil kumar
3 Lakshmi
4 Vasuki
5 Prhvi
6 Charan
7 VTU
8 Beguam
OUTPUT
$sh 9a.sh
clear
echo -e " Enter the filename :\c"
read fn
for ln in `cat $fn`
do
lgth=`echo $ln | wc -c`
lgth=`expr $lgth - 1`
s=1;e=40
if [ $lgth -gt 40 ]
then
while [ $lgth -gt 40 ]
do
echo "`echo $ln | cut -c $s-$e`\\"
s=`expr $e + 1`
e=`expr $e + 40`
lgth=`expr $lgth - 40`
done
echo $ln | cut -c $s-
else
echo $ln
fi
done
echo "File Folded "
OUTPUT
cat 9b.txt
WriteashellscriptthatfoldslonglinesintocolumnThusanylinethatexceeds40
$sh 9b.sh
Enter the filename:9b.txt
Writeashellscriptthatfoldslonglinesintoc\
olumnThusanylinethatexceeds40
File folded
BEGIN {
FS="-";
printf " Day Month Year\n";
}
{
printf " \n The date is %d-%d-%d\n ", $1,$2,$3;
if (( $1 >=1 && $1 <=31) && ($2 >=1 && $2 <=12))
{
printf "The date is valid\t:";
if($2==1)mon="Jan";
else if($2==2) mon="Feb";
else if($2==3) mon="Mar";
else if($2==4) mon="Apr";
else if($2==5) mon="May";
else if($2==6) mon="Jun";
else if($2==7) mon="Jul";
else if($2==8) mon="Aug";
else if($2==9) mon="Sep";
else if($2==10) mon="Oct";
else if($2==11) mon="Nov";
else if($2==12) mon="Dec";
printf "Date is %s-%d-%d", mon,$1,$3;
}
else
printf " date is invalid\n"
system ("exit");
}
output
cat 10a.txt
21-10-2014
12-12-13
32-12-65
BEGIN {
printf " Program starts\n" ;
}
{
if ( data[$0]++ == 0 )
line[++count]=$0;
}
END {
for (i=1; i<= count; i++)
printf "\n" line[i];
printf " program ends";
}
output:
cat 11b.txt
Ajit
Anil
Lakshmi
Ajit
Anil
Prinka
$awk -f 11b.awk 11b.txt
Ajit
Anil
Lakshmi
Prinka
BEGIN {
printf " Total number of books each category\n"
}
{
b[$1]+=$2
}
END{
for(item in b)
{
printf "%s %s %d \n", item , "=", b[item];
total+=b[item];
}
printf "%s %s %s \n", "total books", "=",total;
}
cat 11a.dat
Electrical 34
Mechanical 67
Electrical 80
Computer 43
Mechanical 65
Civil 25
$awk -f 11a.awk 11a.dat
Civil 25
Mechanical 132
Electrical 114
total Books = 314
BEGIN {
printf " ENo ENAME DOJ EDEPT Esalary";
}
{
if($5 >= 1000)
{
hra=$5*0.20;
da=$5*0.50;
}
else
{
hra=$5*0.15;
da=$5*0.45;
}
gs=$5+hra+da;
printf " %s %s %s %s %d %d \n ",$1,$2,$3,$4,$5,gs
}
output
cat 11b.dat
e101 lakshmi 11/8/14 MCA 15000
e102 akhil 12/9/14 ECE 8000
$awk -f 11b.awk 11b.dat
ENo ENAME DOJ EDEPT Esalary
e101 lakshmi 11/8/14 MCA 15000 25500
e102 akhil 12/9/14 ECE 8000 13600
or
BEGIN { printf “Etner the Bsic 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
}
$awk -f 11b.awk
Enter the Basic Pay:Rs.10000
Gross Salary = Rs.17000