1.
a) Login to the system
b) Use the appropriate command to determine your login shell
c) Use the /etc/passwd file to verify the result of step b
d) Use the who command and redirect the result to a file called myfile1. use the more
command to see the contents of myfile1.
e) Use the date and who commands in sequence (in one line) such that the output of date
will display on the screen and the output of who will be redirected to a file called
myfile2. use the more command to check the contents of myfile2.
f) Write a sed command that deletes the first characters in each line in a file.
$ more myfile2
13csea pts/0 May 20 16:18 (192.168.80.222)
12csea pts/1 May 20 16:13 (192.168.80.222)
13cseb pts/2 May 20 16:13 (192.168.80.222)
myfile2: END
2. a) Write sed command that deletes the first character in each line in a file
b) Write a sed command that deletes the character before the last character in each line in a
file.
c ) Write a sed command that swaps the first and second words in each line in a file.
$ cat temp
1425 15.65 Ravi
1450 21.86 Raju
4320 26.27 Ramu
6830 36.15 Sita
b)deletes the character before the last character in each line in a file.
$ cat temp
1425 15.65 Ravi
1450 21.86 Raju
4320 26.27 Ramu
6830 36.15 Sita
$ cat names
Water:melon
Butter:milk
WEEK4:
a) Pipe your /etc/passwd file to awk, and print out the home directory of each user
b) Develop and interactive grep script that asks for a word and a file name and then tells
how many lines containing that word
c) Repeat
d) Part using awk
a) The password file has fields delimited by colons and home drectory is 6th field
$ awk < /etc/passwd -F: '{ print $6 }'
b) $cat data
welcome to nec
hai nec nec
butter milk
Shell script:
echo Enter a word
read word
echo Enter file name
read file
nol=$(grep -c $word $file)
echo $nol lines $word present in $file
output:
Enter a word
nec
Enter file name
data
2 lines nec present in data
c) & d)
$ cat employee
1234|singh Chandan|CAO|34000
3456|srivastav susant|HOD|45000
7654|kumar ramesh|DIRECTOR|56000
2233|gunjan shilpa|PROGRAMMER|23000
5556|satpathi suresh|SYSTEM MANAGER|45600
$ awk -F"|" '{ print $2, $4}' < employee
singh Chandan 34000
srivastav susant 45000
kumar ramesh 56000
gunjan shilpa 23000
satpathi suresh 45600
WEEK5:
a) Write a shell script that takes a command line argument and reports on whether it is a
directory , a file, or something else.
b) Write a shell script that accepts one or more filenames as arguments and converts all of
them to uppercase, provided that they exist in the current directory.
c) Write a shell script that determines the period for which a specified user is working on
the system.
a) Shell script:
for file in $*
do
if [ -f $file ]
then
echo $file is a file
elif [ -d $file ]
then
echo $file is a directory
else
$file is neither directory nor file
fi
done
WEEK6:
A) Write a shell script that accepts a filename starting and ending line numbers as arguments and
displays all the lines between the given line numbers
B) Write a shell script that deletes all lines containing specified word in one or more files
supplied as arguments to it.
a) Shell script:
echo Enter file name
read file
echo Enter starting line nunber
read start
echo Enter ending line number
read end
line=`expr $start + 1`
while [ $line -lt $end ]
do
head -$line $file | tail -1
line=`expr $line + 1`
done
output:$cat data
hai nec
hello nec
welcome to nec
Foss loab
programs
$sh 6a.sh
Enter file name
data
Enter starting line nunber
1
Enter ending line number
5
hello nec
welcome to nec
Foss loab
b) Shell script:
if [ $# -eq 0 ]
then
echo "please enter one more file name"
else
echo enter word to delete
read word
echo contents of file after deletion of $word
for file in $*
do
sed "/$word/d" $file |tee tmp
mv tmp $file
done
fi
WEEK7:
a) Write a shell script that computes the gross salary of a Employee according to folowing rules:
I) If basic salary is < 1500 the hra = 10% of the basic salary and da = 90% of the basic
II) If basic salary is >=1500 then hra=rs.500 and da=98% of the basic. The basic salary is entered
interactively through the keyboard.
b) write a shell script that accepts two integers as its arguments and computes the value of first
number raised to the power of second number.
output:$sh 7a.sh
Enter basic salary
3000
Gross salary:6440
output:$sh 7b.sh
enter two numbers
12
2
12 raised to the power of 2 is 144
WEEK8:
a) Write an interactive file-handling shell program. Let it offer the user the choice of
copying, removing, renaming or lining files. Once the user has made a choice, have the
program ask the user for the necessary information, such as the file name, new file name
and so on.
b) Write shell script that takes a login name as command line argument and reports when
person logs in
c) Write a shell script which receives two file names as arguments. It should check whether
the two file contents are same or not. If they are same then second file should be deleted.
a) Shell script:
echo 1.copy 2.Remove 3.Rename 4.Link
echo Enter your choice
read choice
case $choice in
1)echo Enter source file name
read source
echo Enter destination file name
read destination
cp $source $destination
echo $source is copied to $destination
;;
2)echo Enter file name you want to delete
read file
rm $file
$file is deleted
;;
3)echo Enter the file name you want to rename
read source
echo Enter new name
read new
mv $source $new
echo $source is renamed as $new
;;
4)echo Enter file name to link
read source
echo Enter anothe r file name
read target
var=$(pwd)
ln $source $var/$target
i1=$(ls -i $source|cut -c1-6)
i2=$(ls -i $var/$target|cut -c1-6)
if [ $i1 -eq $i2 ]
then
echo $source and $target are linked and have same inode number $i1
fi
;;
*)echo Invalid choice
;;
esac
output:$sh 8a.sh
1.copy 2.Remove 3.Rename 4.Link
Enter your choice
4
Enter file name to link
option.sh
Enter anothe r file name
eg.sh
option.sh and eg.sh are linked and have same
inode number 295344
b) Shell script
echo Enter name of user
read name
a=$(who | grep -i $name | cut -c23-38)
if [ $? -eq 0 ]
then
echo $name is logged at $a
fi
output:$sh 8b.sh
Enter name of user
student
student is logged at 2015-03-22 21:11
c) Shell script:
echo Enter file name
read f1
echo Enter another file name to compare
read f2
cmp $f1 $f2
exitstatus=$?
echo Exit status is $exitstatus
if [ $exitstatus -eq 0 ]
then
echo Two files are identical
rm $f2
echo $f2 is deleted
else
echo $f1 and $f2 are not identical
fi
output:$sh 8c.sh
Enter file name
cmp.sh
Enter another file name to compare
cmp1.sh
Exit status is 0
Two files are identical
cmp1.sh is deleted
WEEK 9:
a) Write a shell script that displays a list of all files in the current directory to which the user
has read, write and execute permissions
b) develop an interactive shell script that asks for a word ad filename and then tells how
many times that word occurred in a file.
c) Write a shell script to perform following string operations:
i) To extract a sub-string from a given string
ii) to find length of given string
a) Shell script:
echo Files which have read write and execute permissions
for file in `ls`
do
if [ -r $file a w $file a x $file]
then
echo $file
fi
done
output:$cat data
nec nec
hai nec
hello nec
welcome
$sh 9b.sh
Enter a file name
data
Enter a word
nec
wrd count is
4
c) Shell script:
echo Enter a string
read s1
length="${#s1}"
echo Length of sub string is $length
echo Enter the value of position from which you want to extract
read i
echo Enter the length of string to extract
read n
while [ $i -le $n ]
do
h=`expr $s1 | cut -c $i`
i=`expr $i + 1`
echo -n $h '\b'
done
echo "\n"
output: Enter a string
fosslabprograms
Length of sub string is 15
Enter the value of position from which you
want to extract
4
Enter the length of string to extract
5
slabp
WEEK 10:
Write a C program that takes one or more file or directory names as command line input and
reports the following information on the file:
a) file type
b) number of links
c) read, write and execute permission
d) Time of last access (use stat/fstat system calls)
#include <stdio.h>
#include <sys/stat.h>
int main(int argc, char **argv)
{
struct stat statbuf;
int k;
for(k=1;k<argc;k++)
{
if(lstat(argv[1], &statbuf)==1)
printf("can.t stat file\n");
else
{
printf("File : %s\n",argv[k]);
printf("Type and permission : %o\n",statbuf.st_mode);
printf("Number of links %d\n",statbuf.st_nlink);
printf("Time of last access: %s\n",ctime(&statbuf.st_atime));
}
}
}
The execution and output:
$ cc 10.c
$ ./a.out 8a 8b
File : 8a.sh
Type and permission : 100744
Number of links 1
Time of last access: Fri May 23 16:45:45 2008
File : 8b.sh
Type and permission : 100744
Number of links 1
Time of last access: Fri May 23 16:45:45 2008
Week11:
Write C programs that simulate the following UNIX command
a) mv b)cp (use system calls)
a)mv command:
#include <stdio.h>
#include<unistd.h> #include<fcntl.h>
main(int argc,char *argv[])
{
char buff[50];
int r,fd1,fd2;
fd1=open(argv[1],O_RDONLY);
fd2=open(argv[2],O_CREAT|O_EXCL|O_WRONLY,0733);
for(;;)
{
r=read(fd1,buff,50);
if(r==0) break;
write(fd2,buff,r);
}
remove(fd1);
close(fd2);
}
WEEK 12:
Write a C program that simulate ls command.
ls command:
#include <dirent.h>
#include <stdio.h>
int main(int argc, char **argv)
{
DIR *dir;
struct dirent *direntry;
if((dir=opendir(argv[1]))==NULL)
puts("Directory not found..");
while((direntry=readdir(dir))!=NULL)
printf("%10d%s\n", direntry->d_ino,direntry->d_name);
closedir(dir);
return (0);
}
The execution and output:
$ cc 12.c
$ ./a.out btech2
28675 .
16390 ..
28676 cut1
28677 cut2
28690 cut3
28739 emp
28740 hello
28741 myfile.txt
28742 myfile1
28743 myfile2
28744 mytable
28745 mytable1
28746 names
28747 t
28748 table
28749 temp
28750 week4b
28751 week5a
$ ./a.out btech1
Directory not found
$
1)Write a shell script to check whether a particular user has logged in or not.If he has logged
in,also check whether he has eligibility to receive a message or not.
Shell script:
a=$(who | grep -i $1 )
if [ $? -eq 1 ]
then
echo He is not logged in
exit
else
v=whoami
if [ $1 = $v ]
then
echo $1 is logged in but $1 cannot receive message
else
echo $1 is logged in $1 can receive message
fi
fi
output:$whoami
user
$sh 13.1.sh student
student is logged in but student cannot receive
message
2)write a shell script to accept the name of the file from standard input and perform the following
tests on it
a)File executable b)File readable c)File writable
d)Both readable and writable
Shell script:
if [ -x $1 ]
then
echo "The file has executable permission"
else
echo "The file has no executable permission"
fi
if [ -r $1 ]
then
echo "The file has readable permission"
r=1
else
echo "The file has no redable permission"
r=0
fi
if [ -w $1 ]
then
echo "The file has writable permissinon"
w=1
else
echo "The file has no writable permission"
w=0
fi
if [ r eq 1 a w eq 1 ]
then
echo "The file has both readable and weritable permission"
elif [ r eq 0 a w eq 1 ]
then
echo "The file has no readable but have writable permission"
elif [ r eq 1 ]
then
echo The file has readable but no writable permission
else
echo The file has no readable and writable permisssions
fi
3)write a shell script which will display the username and terminal name who login recently into
the unix system
Shell script:
echo current user is
whoami
echo Terminal name is
tty
output:$sh 13.3.sh
current user is
student
Terminal name is
/dev/pts/0
Shell script:
count=0
for f in `ls`
do
count=`echo $count + 1 | bc`
done
echo No of files in current directory is $count
output:$sh 13.4.sh
No of files in current directory is 50
Shell script:
echo "Enter a number :"
read num
i=1
count=0
while [ $i -lt $num ]
do
d=`expr $num % $i`
if [ $d -eq 0 ]
then
count=`expr $count + $i`
fi
i=`expr $i + 1`
done
if test $count -eq $num
then
echo " $num is a perfect number"
else
echo " $num is not a perfect number"
fi
output:$sh 13.5.sh
Enter a number :
28
28 is a perfect number
6)write a menu driven shell script to copy,edit,rename and delete a file
Shell script:
echo "1.Copy 2.Rename 3.Delete"
echo Enter your choice
read choice
case $choice in
1) cp $1 $2
echo $1 is copied to $2
;;
2)mv $1 $2
echo $1 is renamed as $2
;;
3)rm $1
echo $1 is deleted
;;
*)echo Ivalid choice
;;
esac
output: $sh 13.6.sh vowel.data
1.Copy 2.Rename 3.Delete
Enter your choice
2
vowel.data is renamed as vow.data
7)write a shell script for concatenation of two strings
Shell script:
echo "enter twos strings"
read s1
read s2
echo concatenated string is $s1$s2
output: $sh 13.7.sh
enter two strings
butter
milk
concatenated string is buttermilk
8)write a shell script which will display the Fibonacci series up to a given number of argument
Shell script:
echo "Enter length of Fibonacci series"
read length
a=0
b=1
i=0
echo Fibonacci series
while [ $i -lt $length ]
do
c=`expr $a + $b`
echo $c
a=$b
b=$c
i=`expr $i + 1`
done
output: $sh 13.8.sh
Enter length of Fibonacci series
5
Fibonacci series
1
2
3
5
8
9) write a shell script to accept number, name, marks in 5 subjects. Find total, average and grade.
Display the result of student and store in a file called stu.dat
Rules: avg>=80 then grade A
Avg<80 && Avg>=70 then grade B
Avg<70 && Avg>=60 then grade C
Avg<60 && Avg >=50 then grade D
Avg<50 && Avg>=40 then grade E
Else grade F
Shell script:
echo "Enter student number"
read number
echo "Enter student name"
read name
echo "Maximum marks for each subject:100"
echo "Enter subject marks"
echo "First subject:"
read s1
echo "Second subject:"
read s2
echo "Third subject:"
read s3
echo "Fourth subject:"
read s4
echo "Fifth subject:"
read s5
total=`expr $s1 + $s2 + $s3 + $s4 + $s5`
echo "Total=$total"
average=`echo $total / 5 | bc`
echo "Average=$average"
if [ $s1 -lt 40 -o $s2 -lt 40 -o $s3 -lt 40 -o $s4 -lt 40 -o $s5 -lt
40 ]
then
echo "Fail"
exit
elif [ $average -ge 80 ]
then
echo "Grade=A"
elif [ $average -lt 80 -a $average -ge 70 ]
then
echo "Grade=B"
elif [ $average -lt 70 -a $average -ge 60 ]
then
echo "Grade=C"
elif [ $average -lt 60 -a $average -ge 50 ]
then
echo "Grade=D"
elif [ $average -lt 50 -a $average -ge 40 ]
then
echo "Grade=E"
else
echo "Grade=F"
fi
output: $sh 13.9.sh
Enter student number
12345
Enter student name
xxxxx
Maximum marks for each subject:100
Enter subject marks
First subject:
12
Second subject:
56
Third subject:
78
Fourth subject:
87
Fifth subject:
69
Total=302
Average=60
Fail
10)write a shell script to accept empno, empname, basic.Find DA, HRA, TA, PF using following
rules.Display empno, empname, basic, DA, HRA, PF, TA, GROSSSAL and NETSAL. Also store
all details in a file called emp.dat
Rules:HRA is 18% of basic if basic>1500 otherwise 550
DA is 35% of basic
PF is 13% of basic
IT is 14% of basic
TA is 10% of basic
[bcdfghjklmnpqrstvwxyz])
c=`expr $c + 1`
;;
[BCDFGHJKLMNPQRSTVWXYZ])
c=`expr $c + 1`
;;
[0-9])
n=`expr $n + 1`
;;
[" "])
w=`expr $w + 1`
;;
*)
s=`expr $s + 1`
;;
esac
i=`expr $i + 1`
done
echo NO of vowels are $v
echo No of consonants are $c
echo No of digits are $n
echo No of white spaces are $w
echo No of special characters are $s
output:$sh 13.29.sh
Input a string
nec12345!@#$%
length of string is 13
NO of vowels are 1
No of consonants are 2
No of digits are 5
No of white spaces are 0
No of special characters are 5
30)write a shell script to lock terminal
Shell script:
stty .echo
while true
do
clear
echo "Enter the paasword"
read pass1
echo "Re enter the password"
read pass2
if [ $pass1 = $pass2 ]
then
echo "Terminal locked"
echo "To unlock enter the password"
pass1=""
until [ "$pass1" = "$pass2" ]
do
read pass1
done
echo "Terminal unlocked"
stty echo
exit
else
echo "Password mismatch retype it"
fi
done
output:$sh 13.30.sh
Enter the paasword
nec
Re enter the password
nec
Terminal locked
To unlock enter the password
Ne
NEC
nec
Terminal unlocked
$sh 13.30.sh
Enter the paasword
nec
Re enter the password
Ne
Password mismatch retype it