Professional Documents
Culture Documents
Unix Lab Manual12
Unix Lab Manual12
in reverse order.
echo "Checking the number of arguments"
if [ $# -eq 0 ]
then
echo "No arguments supplied"
exit
fi
n=$#
while [ $n -gt 0 ]
do
eval echo \$$n
n=`expr $n - 1`
done
OUTPUT:
[root@localhost unix]$
Checking the number of
No arguments supplied
[root@localhost unix]$
Checking the number of
great
are
indians
[root@localhost unix]$
sh prg01.sh
arguments
sh prg01.sh indians are great
arguments
2) Write a shell script, which takes two filenames as command line arguments
and checks, whether two files have identical file permissions and print. If not,
print filenames and their permissions separately.
if [ $# -eq 0 ]
then
echo "No filename given"
exit
elif [ $# -eq 1 ]
then
echo "Only one filename is given"
exit
else
echo "Two filenames given"
fi
if [ ! -e $1 -a ! -e $2 ]
then
echo "Both files does not exist"
exit
fi
if [ !
then
echo
exit
fi
if [ !
then
echo
exit
fi
-e $1 ]
"File 1 does not exist"
-e $2 ]
"File 2 does not exist"
OUTPUT:
[mca3a6@localhost mca3a6]$sh prg2.sh
Insufficient number of arguments
[root@localhost unix]$sh prg2.sh a1
only one file name is given,Insufficient[root@localhost
unix]$
[root@localhost unix]$sh prg2.sh f1 f2
Two file names are given
Two files exists
Two file permissions are same
File permission: rw-rw-r-[root@localhost unix]$chmod u+x f2
[root@localhost unix]$sh prg2.sh f1 f2
Two file names are given
Two files exists
Different file permissions
File1: rw-rw-r-File2: rwxrw-r-[root@localhost unix]$ _
3) Write a shell script to check whether the given string is palindrome or not.
echo "Enter a string:- "
read str
len=`echo $str | wc -c`
len=`expr $len - 1`
i=1
while [ $len -ge $i ]
do
ch=`echo $str | cut -c $i`
ch1=`echo $str | cut -c $len`
if [ $ch != $ch1 ]
then
echo "The given string is not a palindrome"
exit
fi
len=`expr $len - 1`
i=`expr $i + 1`
done
OUTPUT:
[mca3a6@localho st mca3a6]$ sh prg03.sh
Enter any string
madam
The entered string is palindrome
[mca3a6@localho st mca3a6]$ sh prg03.sh
Enter any string
sir
The string is not a palindrome
[mca3a6@localho st mca3a6]$
4) Write a shell script to do the following actions:a) Accept two filenames as an argument.
b) Sort both the files to temporary file and merge the sorted file to get the
final output. Finally remove the temporary file.
if [ $# -ne 2 ]
then
echo "Invalid arguments"
exit
fi
sort $1>tempfile1
sort $2>tempfile2
echo "Sorted Merged file is"
sort -m tempfile1 tempfile2 | sort -n
rm tempfile1 tempfile2
OUTPUT:
[root@localhost unix]$ sh file.sh
Invalid arguements
[root@localhost unix]$ sh file.sh mat
Invalid arguements
1
2
2
[[root@localhost unix]$ sh file.sh mat mat1
1
123
2
2
456
789
[root@localhost unix]$ sh file.sh f1 f2
hi
how
[root@localhost unix]$
5) Write a shell script to accept a number and print the digits of that number
in reverse order.
echo "Enter a number: "
read num
rev=0
if [ $num -lt 0 ]
then
echo "Positive number has to be entered"
exit
fi
while [ $num -gt 0 ]
do
rem=`expr $num % 10`
rev=`expr $rev \* 10 + $rem`
num=`expr $num \/ 10`
done
echo "The reverse of the given number is:"
echo $rev
OUTPUT:
[root@localhost unix]$ sh prg04.sh
Enter a positive number
6
usage!Input atleast two digit number
[root@localhost unix]$ sh prg04.sh
Enter a positive number
56
Reverse of the given number 56 is: 65
[root@localhost unix]$
OUTPUT:
[root@localhost unix]$ sh terloc.sh
enter password
confirm password
confirmation mistake
[root@localhost unix]$ sh terloc.sh
enter password
confirm password
screen locked
to open the lock , re-enter the password
wrong password
try again re-enter the password
screen unlocked
[root@localhost unix]$
7) Write a shell script which accepts command line arguments and checks
whether it is a valid login name or not. If it is valid, print the login name and
home directory.
if [ $# -eq 0 ]
then
echo "No arguments"
exit
else
for x in $*
do
if grep "^$x:" /etc/passwd>temp
then
y=`cut -d ":" -f 6 temp`
echo "Login name= " $x
echo "Home directory= " $y
else
echo "Login name does not exist"
fi
done
fi
OUTPUT:
[root@localhost unix]$ sh prg05.sh
No arguments
[root@localhost unix]$ sh prg05.sh mca3a10
LOGIN NAME:= mca3a10
DIR NAME:= /home/mca3a10
[root@localhost unix]$ sh prg05.sh mca
Login doesnt exist
[root@localhost unix]$
8) Write a shell script to determine the number of links of the given file name
as command line arguments.
if [ $# -eq 0 ]
then
echo "Insufficient data"
echo "Usage: Script name, filename, directory"
exit
elif [ $# -eq 2 ]
then
dir=$2
else
dir=`pwd`
fi
cd $dir
if [ ! -e $1 ]
then
echo "File $1 does not exist"
exit
fi
file=$1
set -- `ls -l $file`
link=$2
if [ $link -eq 1 ]
then
echo "File has no links"
else
echo "File has $link links"
fi
set -- `ls -i $file`
inode=$1
find $dir -inum $inode -print
OUTPUT:
[root@localhost unix]$ sh links.sh 123
123 does not exist
[root@localhost unix]$ sh links.sh p.sh
ls: file: No such file or directory
links.sh: [: -eq: unary operator expected
File has links
/home/mca3a6/p.sh
/home/mca3a6/q.sh
/home/mca3a6/r.sh
[root@localhost unix]$ sh links.sh prg02.sh
prg02.sh does not exist
[root@localhost unix]$
10) Write an AWK shell script that folds long line into new colomns. Thus
th
any line that exceeds 5 characters must be broken into after 5 Character
and / be appended as an indication of folding and processing is to be
continued with residue.
awk '
{
str=$0
while(length(str)>5)
{
printf("%s\\\n",substr(str,1,5))
str=substr(str,6,length(str)-5);
}
print str
} ' $@
OUTPUT:
[root@localhost unix]$ sh prg11.sh
all the students in our college read well told the teacher
all t\
he st\
udent\
s in \
our c\
olleg\
e rea\
d wel\
l tol\
d the\
teac\
her
[root@localhost unix]$
OUTPUT:
[root@localhost unix]$ awk -f prg8.awk mat1
Entered matrix is
1
2
3
4
5
6
7
8
9
Transpose of matrix is
1
4
7
2
5
8
3
6
9
[root@localhost unix]$ cat>mat2
10
20 30
40
50 60
70
80 90
[root@localhost unix]$ cat mat2
10
20 30
40
50 60
70
80 90
[root@localhost unix]$ awk -f prg8.awk mat2
Entered matrix is
10
20 30
40
50 60
70
80 90
Transpose of matrix is
10
40 70
20
50 80
30
60 90
[root@localhost unix]$
12) Write a shell program which accepts command line argument and creates
directoris in hierarchical order.
if [ $# -eq 0 ]
then
echo "Insufficient Arguments"
exit
fi
IFS=`/`
for dirname in $*
do
mkdir $dirname
cd $dirname
done
OUTPUT:
[root@localhost unix]$ sh prg9.sh
Insufficient Arguments
[root@localhost unix]$ sh prg9.sh bangalore/rnsit/mca
[root@localhost unix]$ cd bangalore
[root@localhost bangalore]$ ls
rnsit
[root@localhost bangalore]$ cd rnsit
[root@localhost rnsit]$ ls
mca
[root@localhost rnsit]$
OUTPUT:
[root@localhost unix]$ lex lexexprn.l
[root@localhost unix]$ cc lex.yy.c -ll
[root@localhost unix]$./a.out
(3+1)
VALID EXPRN
NUMBER OF OPERATORS : 1
NUMBER OF OPERANDS : 2[mca3a8@localhost mca3a8]$ ./a.out
3*%
INVALID
[root@localhost unix]$
OUTPUT:
[root@localhost unix]$ lex lexsimp.l
[root@localhost unix]$ cc lex.yy.c -ll
[root@localhost unix]$ ./a.out
me and my friend went to the market
me my friend went to the market
ITS A COMPUND STATEMENT
[root@localhost unix]$ ./a.out
I am a writer
I am a writer
ITS A SIMPLE STATEMENT
[root@localhost unix]$
%{
int pcount=0,scount=0;
%}
%%
printf {pcount++,fprintf(yyout,"write ");}
scanf {scount++,fprintf(yyout,"read ");}
%%
main()
{
yyin = fopen("dee.c","r");
yyout = fopen("dee2.c","w");
yylex();
printf("NUMBER OF PRINTF = %d\n NUMBER OF SCANF =
%d",pcount,scount);
}
OUTPUT:
[root@localhost unix]$ lex lexcountpfsf.l
[root@localhost unix]$ cc lex.yy.c -ll
[root@localhost unix]$./a.out
NUMBER OF PRINTF = 2
NUMBER OF SCANF = 1
[mca3a8@localhost mca3a8]$ cat dee2.c
#include(stdio.h)
main()
{
int a,b;
write ("ENTER 2 NUM\n");
read ("%d %d",&a,&b);
write ("SUM = %d",a+b);
}
[root@localhost unix]$
#include<stdio.h>
%}
%token a b
%%
st:st reca endb '\n' {printf("STRING BELONGS TO GRAMMER");}
|
|error '\n'{yyerror("DOES NOT BELONG TO GRAMMER");yyerrok;}
reca:enda reca
|
;
enda:a;
endb:b;
%%
main()
{
printf("Enter the text :");
yyparse();
}
yylex()
{
char c;
while((c=getchar())==' ');
if(c=='a')return a;
if(c=='b')return b;
return c;
}
yyerror(char *s)
{
printf("%s",s);
}
OUTPUT:
[root@localhost unix]$ yacc -d yacc1.y
[root@localhost unix]$cc y.tab.c -ll
[root@localhost unix]$ ./a.out
Enter the text :a
syntax errorDOES NOT BELONG TO GRAMMERb
STRING BELONGS TO GRAMMERaaaaab
STRING BELONGS TO GRAMMERba
syntax errorDOES NOT BELONG TO GRAMMER
[root@localhost unix]$
OUTPUT:
[root@localhost unix]$ yacc yacc2.y
[root@localhost unix]$ cc y.tab.c -ll
[root@localhost unix]$ ./a.out
Enter the text :aab
syntax errorDOES NOT BELONG TO THE GRAMMARaabb
STRING BELONG TO GRAMMAR abv
syntax errorDOES NOT BELONG TO THE GRAMMARab
STRING BELONG TO GRAMMAR baa
syntax errorDOES NOT BELONG TO THE GRAMMAR
WHR N VALUE IS 0
[root@localhost unix]$
return num;
}
return ch;
}
yyerror(char *s)
{
printf("%s",s);
}
OUTPUT:
[root@localhost yacc]$ yacc expr.y
[root@localhost yacc]$ cc y.tab.c -ll
[root@localhost yacc]$ ./a.out
enter an expression to evaluate:4+5*7
value is = 39.000000
[root@localhost yacc]$ ./a.out
enter an expression to evaluate:6/4
value is = 1.500000
[root@localhost yacc]$ ./a.out
enter an expression to evaluate:9/0
division by zero!
[root@localhost yacc]$
;
recld:let recld
|dig recld
|let
|dig
;
%%
yylex()
{
char ch;
while((ch=getchar())==' ');
if(isalpha(ch))
return let;
if(isdigit(ch))
return dig;
return ch;
}
yyerror(char *s)
{
printf("%s",s);
}
main()
{
printf("ENTER THE STRING\n");
yyparse();
}
OUTPUT:
[root@localhost yacc]$ yacc yacc4.y
[root@localhost yacc]$ cc y.tab.c -ll
[root@localhost yacc]$ ./a.out
ENTER THE STRING
a4
ACCEPTED
[root@localhost yacc]$ ./a.out
ENTER THE STRING
7er
syntax errorINVALID[root@localhost yacc]$ ./a.out
ENTER THE STRING
$l
syntax errorINVALID[root@localhost yacc]$ ./a.out
ENTER THE STRING
sum
ACCEPTED
[root@localhost yacc]$
YACC PROGRAM:%{
int v=1;
%}
%token
IF STAT CONDITION ELSE
%nonassoc IF
%nonassoc ELSE
%%
ifstruct: IF expr state1
|IF expr state1 ELSE state1 %prec ELSE
;
state1: stmt
|"{"state"}"
|ifstruct %prec IF
;
state: stmt|stmt state
|ifstruct
|ifstruct state
;
expr: "("CONDITION")"
|"("STAT")"
;
stmt: STAT ";"
|";"
;
%%
int yyerror(char *s)
{
v=0;
}
main()
{
yyparse();
if(v)
printf("valid");
else
printf("invalid");
}
OUTPUT: