Unix Lab Manual For JNTU K
Unix Lab Manual For JNTU K
Unix Lab Manual For JNTU K
INDEX
3
2 Introduction About Lab
5
3 Guidelines To Students
7
4 List of Syllabus Programs (JNTU)
10
5 Basic Unix Commands
19
6 Solutions For Programs
Bibliography 55
7
LAB OBJECTIVE
Upon successful completion of this Lab the student will be able to:
1. Demonstrate how to use the following Bourne Shell commands: cat, grep, ls,
more, ps, chmod, finger, ftp, etc.
2. Use the following Bourne Shell constructs: test, if then, if then else, if then
elif, for, while, until, and case.
3. Learn tracing mechanisms (for debugging), user variables, Bourne Shell
variables, read-only variables, positional parameters, reading input to a Bourne
Shell script, command substitution, comments, and exporting variables. In
addition, test on numeric values, test on file type, and test on character strings
are covered.
4. Copy, move, and delete files and directories
5. Write moderately complex Shell scripts.
RAM : 1 GB
Software
All systems are configured in DUAL BOOT mode i.e., Students can boot from
Windows XP or Linux as per their lab requirement.
This is very useful for students because they are familiar with different
Operating Systems so that they can execute their programs in different
programming environments.
Oracle 9i client version is installed in all systems. On the server, account for
each student has been created.
This is very useful because students can save their work ( scenarios’,
pl/sql programs, data related projects ,etc) in their own accounts. Each student
work is safe and secure from other students.
Latest Technologies like DOT NET and J2EE are installed in some
systems. Before submitting their final project, they can start doing mini
project from 2nd year onwards.
Systems are assigned numbers and same system is allotted for students when
they do the lab.
Guidelines to Students
There are two ways you can execute your shell scripts. Once you have created a script
file:
Method 1
Pass the file as an argument to the shell that you want to interpret your script.
For example, the script file show has the following lines
Step 2: To run the script, pass the filename as an argument to the sh (shell )
$ sh show
Here is the date and time
Sat jun 03 13:40:15 PST 2006
Method 2:
Make your script executable using the chmod command.
When we create a file, by default it is created with read and write permission turned
on and execute permission turned off. A file can be made executable using chmod.
For example, the script file show has the following lines
$ show
Here is the date and time
Sat jun 03 13:40:15 PST 2006
Step 1: Use an editor, such as vi, ex, or ed to write the program. The name of the file
containing the program should end in .c.
main()
{
printf(“ welcome to GNEC “);
}
$ cc show.c
If the program is okay, the compiled version is placed in a file called a.out
$ a.out
Welcome to GNEC
WEEK1
Session 1
Session 2
WEEK2
WEEK3
d) uses the who command 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 my file2.Use the more command to check the
contents of myfile2.
a) write a 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 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
c) Repeat
d) Part using awk
WEEK5
a) Write A shell script that takes a command –line argument and reports on whether it
is directry ,a file, or something else
b) Write a shell script that accepts one or more file name as a arguments and converts
all of then to uppercase, provided they exits in the current directory
c) Write a shell script that determines the period for which a specified user is working
on the system
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
b) write a shell script that deletes all lines containing a specified word I one or
more files supplied as arguments to it.
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
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
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.
b) Write a shell script that takes a login name as command –line argument and
reports when that person logs in
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.
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 script that asks for a word and file name and then tells
how many times that word occurred in the file.
c) Write a shell script to perform the following string operations.
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.
1) file type
2) number of links
3) read, write and execute permissions
4) time of last access
WEEK 11
WEEK 12
Command CAT
Syntax cat [argument] [specific file]
Description “cat" is short for concatenate. This command is used
to create, view and concatenate files.
Examples cat /etc/passwd
cat /etc/profile
ls -a
ls -l
ls -al
ls -al /usr
ls -ld /usr
Command Mv
Syntax mv [options] sources target
mv Chapter1 garbage
mv Chapter1 /tmp
mv tmp tmp.old
-f, --force
ignore nonexistent files, never prompt
-I, --interactive
prompt before any removal
-v, --verbose
explain what is being done
rm *.html
rm index*
rm -r new-novel
Command Cp
Syntax cp [options] file1 file2
copied.
cp /usr/fred/Chapter1 .
cp /usr/fred/Chapter1 /usr/mary
Command Grep
Syntax grep [options] regular expression [files]
grep 'joe' *
Command Mkdir
Syntax mkdir [options] directory name
mkdir /usr/fred/tmp
mkdir -p /home/joe/customer/acme
Command Rmdir
Syntax rmdir [options] directories
Description The "rm" command is used to remove files and
directories. (Warning - be very careful when
removing files and directories!)
Examples rm Chapter1.bad
rm *.html
rm index*
rm -r new-novel
cd /usr/fred
cd /u*/f*
cd
cd -
Command Kill
Syntax kill [options] IDs
Description kill ends one or more process IDs. In order to do this you
must own the process or be designated a privileged user. To
find the process ID of a certain job use ps.
Command Ps
Syntax ps [options]
ps -f
ps -e
ps -ef
ps -ef | more
ps -fu fred
SOLUTIONS
WEEK1
Session 1
Sol:
$ vi
~ 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
WEEK2
Log into the system
Use the cat command to create a file containing the following data. Call it
mutable use tabs to separate the fields
Sol:
a) $ cat myfile
c) $ sort +0 -1 mytable
f) $ cat mytable
WEEK3
Sol:
d. $more myfile2
f. Input file:
$ cat demo
my name is srikanth
I am studying m.tech
I am learning UNIX
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
Solution:
a) pipe ur /etc/passwd file to awk and print out the home directory of each
user.
$vi 1.awk
BEGIN{
while("cat /etc/passwd" | getline)
{
var=index($0,"/home/")
wel =substr($0,var)
print wel
}
Esc:wq
$awk -f 1.awk
/home/cse:/bin/bash
/home/it:/bin/bash
b)Develop an interactive grep script that asks for a word and a file name and
then tells how many lines contain that word.
Input :
$cat demo
my name is srikanth
I am studying m.tech
I am learning UNIX
Code:
$vi week42.sh
Output:
$ sh week42.sh
Enter the word
srikanth
Enter the filename
demo
1
WEEK5
a) Write a shell script that takes a command –line argument and reports on
whether it is directory, a file, or something else.
Code:
$vi week51.sh
if test -d $1
then
echo "$1 is a directory"
elif test -f $1
then
echo "$1 is a file"
else
echo "$1 is not a file or a directory"
fi
Output:
b) Write a shell script that accepts one or more file name as arguments and
converts all of them to uppercase, provided they exist in the current directory.
Code:
$vi week52.sh
clear
for file1 in $* ;
do
if [ ! -e file1 ]
then
echo "$file1 doesn't exists"
else
tr "[a-z]" "[A-Z]"< $file1
fi
done
Input:
$cat file2
hello
hru
Output:
c) Write a shell script that determines the period for which a specified user is
working on the system.
Code:
$vi week53.sh
echo "enter user name"
read uname
echo "the user has logged in from"
last $uname
Output:
$sh week53.sh
enter user name
it
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.
Code:
$vi week61.sh
clear
if [ ! -e $mytable ]
then
echo "$mytable does not exist"
else
echo " displayed lines from 2 to 3"
head -3 $1 | tail -2
fi
Input:
$cat mytable
1425 15.65 Ravi
1450 21.86 Raju
4320 26.27 Ramu
6830 36.15 Sita
Output:
b)Write a shell script that deletes all lines containing a specified word in one or
more files supplied as arguments to it.
Code:
Input:
$ cat demo
my name is srikanth
I am studying m.tech
I am learning UNIX
Output:
$sh week62.sh demo
Original File
my name is srikanth
I am studying m.tech
Modified file i.e. lines which do not have word UNIX
my name is srikanth
I am studying m.tech
WEEK7
a)Write a shell script that computes the gross salary of a employee according to
the following rules:
I)If basic salary is < 1500 then HRA =10% of the basic and DA =90% of the
basic.
ii)If basic salary is >=1500 then HRA =Rs500 and DA=98% of the basic
The basic salary is entered interactively through the key board.
Code:
$vi week71.sh
clear
echo " Enter The Basic Salary"
read basic
if [ $basic -lt 1500 ]
then
da=`expr $basic \* 90 / 100`
hra=`expr $basic \* 10 / 100`
else
da=`expr $basic \* 98 / 100`
hra=500
fi
Output:
$sh week71.sh
Enter The Basic Salary
1500
Basic:1500
Gross:3470
DA:1470
HRA:500
b)Write a shell script that accepts two integers as its arguments and computers
the value of first number raised to the power of the second number.
Code:
$sh week72.sh
clear
a=$1
b=$2
pow=1
count=1
if [ ! $b -eq 0 ]
then
while [ $count -le $b ]
do
pow=`expr $pow \* $a`
count=`expr $count + 1`
done
fi
echo "$a power $b is : $pow"
Output:
$sh week72.sh 5 2
5 power 2 is : 25
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.
Code:
$ vi week81.sh
clear
ch=1
while [ $ch -eq 1 ]
do
echo "1.copy"
echo "2.remove"
echo "3.rename"
echo "4.linking file"
echo "enter your choice"
read choice
case "$choice" in
1)echo "enter source file"
read source
echo "enter destination file"
read dest
cp $source $dest
echo "source copied successfully to $dest" ;;
2)echo "enter filename to remove"
read file
rm $file
echo "$file is successfully removed" ;;
3)echo "enter filename to remove"
read file
Output:
$ sh week81.sh
1.copy
2.remove
3.rename
4.linking file
enter your choice
1
enter source file
f2
enter destination file
sree
source copied successfully to sree
do u want to continue \n 1.contine \n 2.stop
(b) Write a shell script that takes a login name as command –line argument
and reports when that person logs in
Code:
$vi week82.sh
clear
who | grep $1 > temp
echo -n "$1 logs at"
cut -c 23-28 < temp
Output:
$ sh week82.sh it
it logs atOct 1
Oct 1
(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.
Code:
$ vi week83.sh
clear
cmp -s $1 $2
if [ $? -eq 0 ]
then
echo "two files are identical"
rm $2
echo "second file is deleted"
else
echo "two files are not identical"
fi
Output:
$ sh week83.sh f2 sree
two files are identical
second file is deleted
$ sh week83.sh f2 demo
two files are not identical
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
Code:
$ vi week91.sh
ls -l |grep "^.rwx"
Output:
$ sh week91.sh
-rwxrwxr-x 1 it it 5202 Sep 30 16:15 a.out
drwxrwxr-x 2 it it 4096 Jul 22 15:58 backup
drwxrwxr-x 2 it it 4096 Jul 22 16:18 cat
drwxr-xr-x 4 it it 4096 Oct 1 11:23 Desktop
drwxrwxr-x 3 it it 4096 Jul 22 16:11 lndir
(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.
Code:
$vi week92.sh
clear
ch=0
while [ $ch -eq 0 ]
do
echo "enter word and filename"
read word file
if test -f $file
then
count=`cat $file | grep -c $word`
echo "found $count times"
else
Output:
$sh week92.sh
enter word and filename
srikanth demo
found 1 times
anymore ? (0-yes,1-no)
1
Code:
$ vi week93.sh
clear
echo "enter string"
read str
echo "enter starting position and no. of characters to be extracted"
read str n char
tot=`expr $start + $char -1`
substr=`echo $str | cut -c $start - $tot`
echo "$substr"
Output:
$ sh week93.sh
enter string
srikanth
enter starting position and no. of characters to be extracted
13
sri
Code:
$vi week94.sh
clear
echo "enter string to find length"
read str
len=`expr "$str"'.*'`
echo "length is:$len"
Output:
$ sh week94.sh
enter string to find length
srikanth
length is:8
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.
1. file type
2. number of links
3. read, write and execute permissions
4. time of last access
PROGRAM
$vi week10.c
#include<stdio.h>
#include<sys/stat.h>
#include<sys/types.h>
int main(int argc,char **argv)
{
struct stat st ;
int k;
for(k=1;k<argc;k++)
{
if((stat(argv[k],&st)==-1))
printf("can't stat file\n");
else
{
printf("\nFile=%s",argv[k]);
printf("\nInode Number: %u",st.st_ino);
printf("\n Owner User ID : %d",st.st_uid);
printf("\nOwner Group ID : %d",st.st_gid);
printf("\nType and permission =%o",st.st_mode);
printf("\nNo. of links=%d",st.st_nlink);
printf("\n Time of last access :%s",ctime(&st.st_atime));
}
}
}
Output:
$ cc week10.c
$ ./a.out week10.c
File=week10.c
Inode Number: 4473752
Owner User ID : 1000
Owner Group ID : 100
Type and permission =100644
No. of links=1
Time of last access :Wed Sep 30 10:33:47 2009
WEEK 11
Success
PROGRAM
$ vi week11b.c
#include<stdio.h>
#include<fcntl.h>
#include<unistd.h>
main(int argc, char **argv)
{
char c;
int n,size,fd1,fd2;
fd1=open(argv[1],O_RDONLY);
fd2=open(argv[2],O_WRONLY);
size=lseek(fd1,-1,SEEK_END);
n=lseek(fd1,0,SEEK_SET);
while(n++ <size)
{
read(fd1,&c,1);
write(fd2,&c,1);
}
}
Output:
$ cc week11b.c
$ ./a.out week11b.c file2
$ cat file2
#include<stdio.h>
#include<fcntl.h>
#include<unistd.h>
main(int argc, char **argv)
{
char c;
int n,size,fd1,fd2;
fd1=open(argv[1],O_RDONLY);
fd2=open(argv[2],O_WRONLY);
size=lseek(fd1,-1,SEEK_END);
n=lseek(fd1,0,SEEK_SET);
while(n++ <size)
{
read(fd1,&c,1);
write(fd2,&c,1);
}
}
WEEK 12
PROGRAM:
#include<dirent.h>
#include<stdio.h>
int main(int argc, char ** argv)
{
DIR *dir;
struct dirent *direnty;
if((dir=opendir(argv[1]))==NULL)
puts("Directory Not found");
while((direnty=readdir(dir))!=NULL)
printf("%10d %s \n",direnty -> d_ino,direnty -> d_name);
closedir(dir);
return(0);
}
Out put:
$ cc week12.c
$ ./a.out sree
4473913 week11.c
4481968 week12.c
4472833 ..
4481966 a.out
4481965 .
4473752 week103.c
4473847 week111.c
BIBLIOGRAPHY
5. UNIX Shell Programming, 4th Edition , Lowell Jay Arthur, Ted Burns
7. The UNIX Operating System, 3rd Edition, by Kaare Christian, Susan Richter