Professional Documents
Culture Documents
SOLUTIONS:
WEEK1
Session 1
Sol:
$ vi myfile.txt
~ Unix is Case Sensitive
~ Never leave the Computer without logging out when you are working in a
time sharing or network environments.
Type <Esc>
: wq myfile
$
Session 2
1. Log into the system
2. Open the file created in session 1
3. Add some text
4. Change some text
5. delete some text
6. Save the changes
7. Logout of the system
Sol:
$ login: <user name>
$ password: ******
$ vi myfile
: wq
Changing Text
Cw
change the current word with new text,
starting with the character under cursor, until <Esc> hit
cNw
change N words beginning with character under cursor, until <Esc> hit;
e.g., c5w changes 5 words
C change (replace) the characters in the current line, until <Esc> hit
Cc change (replace) the entire current line, stopping when <Esc> is hit
change (replace) the next N lines, starting with the current line,
Ncc or cNc
stopping when <Esc> is hit
Deleting Text
dNw
delete N words beginning with character under cursor;
e.g., d5w deletes 5 words
D delete the remainder of the line, starting with current cursor position
* Dd delete entire current line
delete N lines, beginning with the current line;
Ndd or dNd
e.g., 5dd deletes 5 lines
WEEK2
Log into the system
FOSS LAB MANUAL :: DEPARTMENT OF IT :: ALIET
3
Use the cat command to create a file containing the following data. Call it
mytable use tabs to separate the fields
Sol:
b)Use the cat command to create a file containing the following data.
Call it mytable use tabs to separate the fields.
1425 Ravi 15.65
4320 Ramu 26.27
6830 Sita 36.15
1450 Raju 21.86
e)Use the sort command to sort the file mytable according to the first
field. Call the sorted file my table
(same name)
Sol: lp mytable
g)Use the cut and paste commands to swap fields 2 and 3 of mytable.
Call it my table (same name)
Sol:
$cut -f1 mytable> mytab1
Sol: $ lp mytable
WEEK3
$ echo $SHELL
/bin/bash
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.
$cat myfile2 :
InputFile:file1.dat :
UnixisMultiuser OS
Unix was developed by Brian Kernighan and KenThomson
2.a)write a sed command that deletes the first character in each line in a
file
nix is Multiuser OS
nix was developed by Brian Kernighan and KenThomson
b)write a sed command that deletes the character before the last
character in each line in a file.
Unix is Multiuser S
Unix was developed by Brian Kernighan and KenThomsn
c)Write a sed command that swaps the files and second words in each
line in a file
WEEK4
a)pipe ur /etc/passwd file to awk and print out the home directory of
each user.
b)Develop an interactive grep script that asks for a word and a file
name and then tells how many lines contain that word
sol:
Repeat
WEEK5
Sol:
FOSS LAB MANUAL :: DEPARTMENT OF IT :: ALIET
8
elif [ -d $f ]
then
echo "Directory "
else
echo "Something else"
fi
Sol:
Clear
if [ $# -eq 0 ]
then “echo enter the arguments”
exit
fi
for i in $*
do
if [ -f $i ]
then
echo it is a valid file
echo Contents of file before converting
cat $i
echo Contents of file after converting
tr '[a-z]' '[A-Z]' < $i
k=`ls $i | tr '[a-z]' '[A-Z]'`
mv $i $k
echo file $i renamed as $x
ls
Sol:
WEEK6
(a) Write a shell script that accepts a file name starting and ending
line numbers as arguments and displays all the lines between the given
line numbers
Sol:
echo "enter the file name"
read fname
echo "Enter the starting line number "
read s
echo "Enter the ending line number "
read n
sed -n $s,$n\p $fname | cat > newline
cat newline
(b) Write a shell script that deletes all lines containing a specified word
I one or more files supplied as arguments to it.
Sol:
if [ $# -eq 0 ]
then
echo "no arguments passed"
exit
fi
echo "the contents BEFORE deleting"
FOSS LAB MANUAL :: DEPARTMENT OF IT :: ALIET
11
for i in $*
do
echo $i
cat $i
done
echo "Enter the word to be deleted:"
read word
for i in $*
do
grep -vi "$word" $i > temp
mv temp $i
echo "AFTER DELETING"
cat $i
done
WEEK7
a) Write a shell script that computes the gross salary of a employee
according to the following
1) if basic salary is <1500 then HRA 10% of the basic and DA =90%
of the basic
2) if basic salary is >1500 then HRA 500 and DA =98% of the basic
The basic salary is entered interactively through the key board
Sol:
echo enter basic salary
read sal
a=0.1
b=0.8
echo $a
echo "hra is"
hra=`echo 0.1 \* $sal|bc`
echo da is
da=`echo 0.8\*$sal|bc`
gsal=’expr $hra + $da + $sal’
echo $gsal
(b)Write a shell script that accepts two integers as its arguments and
computes the value of first number raised to the power of the second
number
Sol:
WEEK 8
(a) Write an interactive file handling shell program. Let it offer the user the choice of
copying ,removing ,renaming or linking files. Once the use has made a choice, have
the program ask the user for necessary information, such as the file name ,new name
and so on.
echo "1.COPY"
echo "2.RENAME"
echo "3.REMOVE"
echo "4.LINK"
echo "5.EXIT"
echo "Enter your choice"
read ch
case $ch in
1) echo "Enter the sources file"
read s
echo "Enter the destination file "
read d
cp $s $d
;;
2) echo "Enter the old file name"
read of
echo "enter the new file name"
read nf
mv $of $nf
;;
3) echo "Enter file name to delete"
read df
rm $df
;;
4) echo "Enter the file1"
read f1
echo "enter the file2"
read f2
ln $f1 $f2
(b) Write a shell script that takes a login name as command –line
argument and reports when that person logs in
if [ $# -lt 1 ]
then
echo improper usage
echo correct usage is: $0 username
exit
fi
logname=$1
while true
do
who|grep "$logname">/dev/null
if [ $? = 0 ]
then
echo $logname has logged in
echo "$logname">>sh01log.txt
date >>sh01log.txt
echo "Hi" > mesg.txt
echo "$logname" >> mesg.txt
echo "Have a Good Day" >> mesg.txt
mail "$logname" < mesg.txt
exit
else
sleep 60 fi done
(c) Write a shell script which receives two files 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.
if(cmp $1 $2)
then
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
Sol:
for File in *
doif [ -r $File -a -w $File -a -x $File ]
then
echo $File
fi
done
[or]
ls -l|grep '^.rwx'
(b) Develop an interactive script that asks for a word and file name
and then tells how many times that word occurred in the file.
SAME AS 4B QUESTION
echo "MENU"
echo "1.TO EXTRACT A SUBSTRING FROM AGIVEN STRING"
echo "2.TO FIND THE LENGTH OF THE STRING"
echo "ENTER YOUR CHOICE"
read choice
echo "enter a string"
read string
case $choice in
Output:
mode of fileis 33188
size of the file is 65
devicename 2050
inode of file is4945691
no. of links are1
owner of file is694
no.of blocks is8
group owner is694
blocks size of the file is 4096
time of last access is 1427870386
a)
#include<stdio.h>
int main()
{
int i;
char fname[255],fname2[255];
printf("enter old file name");
scanf("%s",&fname);
printf("enter new file name:");
scanf("%s",&fname2);
i=rename(fname,fname2);
if(i==0)
{
printf("%s moved to %s",fname,fname2);
}
else
printf("we cannot move this file");
}
b)cp
#include<stdio.h>
#include<string.h>
int main(int argc,char*argv[])
{
FILE *source,*target;
char ch;
if(argc!=3)
{
printf("Command Error!!Insufficient argument given\n");
return;
}
source=fopen(argv[1],"r");
target=fopen(argv[2],"w");
if(source==NULL || target==NULL)
{
printf("Unable to open..ERROR in opening file!!\n");
return;
FOSS LAB MANUAL :: DEPARTMENT OF IT :: ALIET
18
}
while((ch=fgetc(source))!=EOF)
fputc(ch,target);
printf("Copy is Successful\n");
fclose(source);
fclose(target);
}
WEEK 12
PROGRAM:
#include<stdio.h>
#include<dirent.h>
#include<sys/stat.h>
#include<stdlib.h>
int main(int argc,char *argv[])
{
struct dirent *dire;
DIR *dir;
if((dir=opendir(argv[1]))==NULL)
exit(0);
while((dire=readdir(dir))!=NULL)
printf("%s\n",dire->d_name);
closedir(dir);
}
[or]
Write a shell scripts that finds total no. of users and finds out how
many of them are currently logged in. in Unix
cat /etc/passwd>user.txt
set `wc -l user.txt`
log=`who | wc -l`
echo "There are $1 users in network "
echo "There are $log user logged in right now"
2)Write a shell script to accept the name of the file from standard
input and performs the following tests on it
a)file executable b)file readable c)file writable d)both
readable&writable
3)Write a shell script which will display the username and terminal name
who login recently into the unix system.
FILE="/tmp/count.txt"
echo -n "Enter directory name : "
read dname
SAME AS 8A PROGRAM
13.Write a shell script to delete all files whose size is zero bytes from
current directory.
clear
echo -n "Enter name of the directory :"
read directory
if [ ! -d "$directory" ]
then
echo "Directory does not exist"
else
for i in `find $directory -type f -size 0`
do
rm -i $i
done
fi
15.write a shell script which will display Armstrong numbers from given
arguments.
echo “Enter a number:”
read n
t=$n s=0
while [ $n -gt 0 ]
do
r=`expr $n % 10`
s=`expr $s + $r \* $r \* $r`
n=`expr $n / 10`
done
if [ $s = $t ]
then
echo “$t is an armstrong number”
else
echo “$t is not an armstrong number”
fi
17.write a shell script to display factorial value from given argument list
18.Write a shell script which will find maximum file size in the given
argument list.
clear
hours=`date|cut -c12-13`
if [ $hours -le 12 ]
then
echo "Good Morning"
else
if [ $hours -le 16 ]
then
echo "Good Afternoon"
elif [ $hours -le 20 ]
then
echo "Good Evening"
else
echo "Good Night"
fi
fi
20.Write a shell script to sort the elements in a array using bubble sort
technique.
25.Write an awk program which will find maximum word and its length in
the given input file.
clear
echo "Type any number to generate Mathematical Table"
read number
i=1
while [ $i -le 10 ]
do
echo " $number * $i =`expr $number \* $i ` "
i=`expr $i + 1`
done
clear
echo "Type any String"
read string
case $h in
[AaEeIiOoUu])
nvowels=`expr $nvowels + 1`
;;
[BbCcDdFfGgHhJjKkLlMmNnPpQqRrSsTtVvWwXxYyZz])
nconsonants=`expr $nconsonants + 1`
;;
[0-9]) ndigits=`expr $ndigits + 1`
;;
[@!%$,*+-\&:])
nsymbol=`expr $nsymbol + 1`
;;
esac
done
Clear
stty –echo
echo "enter password to lock the terminal"
read pass1
echo " Re-enter password"
read pass2
if [ "$pass1" = "$pass2" ]
then
echo "system is locked"
echo "enter password to unlock"
trap ``/1 2 3 9 15 18
system is locked
system unlocked
password mismatch
Make file is a utility in Unix to help compile large programs. It helps by only compiling
the portion of the program that has been changed
cmp - Compares two files byte by byte and displays the first
mismatch
diff - tells the changes to be made to make the files identical
Yes, it stands for ‘disk usage’. With the help of this command
you can find the disk capacity and free space of the disk.
The input from a pipe can be combined with the input from a file
. The trick is to use the special symbol “-“ (a hyphen) for those
commands that recognize the hyphen as std input.
In the above command the output from who becomes the std
input to sort , meanwhile sort opens the file […]
Use ‘su’ command. The system asks for password and when
valid entry is made the user gains super user (admin) privileges.
What are the entities that are swapped out of the main
memory while swapping the process out of the main
memory?
Is the Process before and after the swap are the same? Give
reason.
FOSS LAB MANUAL :: DEPARTMENT OF IT :: ALIET
37
Process before swapping is residing in the primary memory in
its original form. The regions (text, data and stack) may not be
occupied fully by the process, there may be few empty slots in
any of the regions and while swapping Kernel do not bother
about the empty slots while swapping the process outAfter
swapping […]
When a program forks and the child finishes before the parent,
the kernel still keeps some of its information about the child in
case the parent might need it - for example, the parent may
need to check the child’s exit status. To be able to get this
information, the parent calls `wait()‘; In the […]
How do you create special files like named pipes and device
files?
A link is a second name (not a file) for a file. Links can be used
to assign more than one name to a file, but cannot be used to
assign a directory more than one name or link filenames on
different computers.
Symbolic link ‘is’ a file that only contains the name of another
file.Operation […]
What is a zombie?
When a program forks and the child finishes before the parent,
the kernel still keeps some of its information about the child in
case the parent might need it - for example, the parent may
need to check the child’s exit status. To be able to get this
information, the parent calls `wait()‘; In the […]
What is a shell?
How do you create special files like named pipes and device
files?
What is a FIFO?
A link is a second name (not a file) for a file. Links can be used
to assign more than one name to a file, but cannot be used to
assign a directory more than one name or link filenames on
different computers.
Symbolic link ‘is’ a file that only contains the name of another
file.Operation […]
What is ‘inode’?
All devices are represented by files called special files that are
located in/dev directory. Thus, device files and other files are
named and accessed in the same way. A ‘regular file’ is just an
ordinary data file in the disk. A ‘block special file’ represents a
device with characteristics similar to a disk (data transfer […]
What is ‘inode’?
All devices are represented by files called special files that are
located in/dev directory. Thus, device files and other files are
named and accessed in the same way. A ‘regular file’ is just an
ordinary data file in the disk. A ‘block special file’ represents a
device with characteristics similar to a disk (data transfer […]
$cat >file means to create a new file $cat file means to open
an existing file.
cat > file it means creating file for file cat file it means used
to display the file content
Which command is
used to delete all
files in the current
directory and all its
sub-directories?
# rm -rf *
Answer
Answered By: Amit Shiknis Date: 12/25/2007
rm -r *
Yes you are correct. It stands for listing the files Chapter
Answer with suffix 1 to 5 but it will display the files in columns as
with-x option.
Device filles are of 2 types --- charcater device file and block
device file
How to switch to a
super user status to
gain privileges?
Use ‘su’ command. The system asks for password and when
Answer valid entry is made the user gains super user (admin)
privileges.
TERM,SHELL, MAIL
$>echo $TERM
ansi
What is redirection?
How to terminate a
process which is
running and the
specialty on
command kill 0?
How to terminate a
process which is
running and the
specialty on
command kill 0?
What is the
difference between
soft link and hard
link in unix
operating system ?
Hard Links :
3. Link has the path for original file and not the contents.
to concatenate
(attach) two
strings?
REFERENCES:
Books:
1)Introduction to UNIX & SHELL programming, M.G. Venkatesh Murthy, Pearson Education.
3)Unix for programmers and users, 3rd edition, Gaham Glass & K. Ables, pearson education.
4)Unix and shell Programming –A text book, B.A. Forouzan & R.F. Giberg, Thomson.