Unix & Shell Programming Manual
Unix & Shell Programming Manual
SESSION 1:
When we return on the system one screen will appear. In this we have to
type 100.0.0.9 then we enter into editor. It asks our details such as
Login : allem530
password:
Here we have to open a file named as my file text when we open the file by
using the above syntax one window will be opened in that we type text as
our wish.
Example: vi myfile.text.
Example: vi530
In our current directory press exit to log out from the commands .In
desktop turn of f computer option will displayed Ok that option and then ok
shutdown option then windows will shutdown and we log out from the system.
SESSION 2:
When we turn on the system one screen will appear. In this we have to type
100.0.0.9 then we enter into editor. It asks
Login : allam530
password:
Syntax: vi filename
It displays the file as follows
To add some text to the file which is already created first we have to open
that file then add text.
vi filename. Text
vi myfile.text
vi myfile.text
esc:WQ
It means that saves the file and quits editing mode then we log out from the
vi Mode.
In our current directory press exit to logout from the commands in desktop
turn off computer option will appear ok that option and then ok shutdown
option then windows will shutdown we logout from the system.
When we turn on the system one screen will appear in this we have to type
100.0.0.9 then we log into the commends it asks
Login
Login : allam530
password:
B) Aim: 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
Cat command is used to create the files Here file name is mytable
Example: cat>mytable
$ cat >mytable
1425 Ravi 15.65
4320 Ramu 26.27
6830 Sita 36.15
1450 Raju 21.86
$ cat mytable
D) Aim: Use the vi command to correct any errors in the file ,mytable
Vi mytable
Use vi command to correct errors
E) Aim: Use the sort command to sort the file mytable according to the
first field. Call the
$ cat mytable
1425 Ravi 15.65
1450 Raju 21.86
4320 Ramu 26.27
6830 Sita 36.15
$ lp mytable
1425 Ravi 15.65
1450 Raju 21.86
4320 Ramu 26.27
6830 Sita 36.15
G) Aim: use the cut and paste commands to swap fields 2 and 3 of
mytable.Call it my table(same name)
the fields are removed by using the syntax as follows the out put
is stored in table
$ cat mytable
$ cat mytable
$ lp mytable
In our current directory type exit to log out from the commands in desktop
turnoff computer option will appear ok that option and then ok shutdown
option then windows will shutdown we logout from the system.
Logout
[ctrl-d] or
Exit
When we turn on the system one screen will appear in this we have to
type 100.0.0.9 then we enter into one window it asks
Login : allam530
password:
If we want to known in which shell you login shell the syntax is as follows
$ echo $SHELL
/bin/bash
Unix contains a system variable shell that identifiers the path to your login
shell
$ cd /
$ ls
bin dev lib media net root srv usr
boot etc home .............. linux tmp
$ cd etc
$ ls
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
....................................................
...................................................
allam:x:501:501:Allamnarasingarao:/home/allam:/bin/bash
D) Aim: use the who command and redirect the result to a file called
myfile1.Use the more command to see the contents of myfile1.
$ who >myfile1
$ more myfile1
allam tty7 2008-08-23 09:08 (:0)
allam pts/1 2008-08-23 09:09 (:0.0)
.......... ....... .......................................
.......... ....... .......................................
E) Aim: 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 mytable
1425 Ravi 15.65
4320 Ramu 26.27
6830 Sita 36.15
1450 Raju 21.86
7820 Anil 23.34
9000 Allam 35.56
D) Aim: Write a sed command that swaps the first and second words in a
file.
A) Aim: pipe your /etc/passwd file to awk , and print out the home directory
of each user.
$ vi home.awk
{
if(match ($0, /^.*home/) > 0)
{
split( $0, user)
split(user[1],homedir, ":")
print homedir[1]
}
}
awk command :
$ cat /etc/passwd | awk -f home.awk
Administrator
Guest
sys1
cse
cse123
it
cse501
cse502
it1201
it1202
$ vi grep.sh
echo "Enter the pattern to be searched: "
read pattern
echo "Enter the file to be used: "
read filename
echo "Searching for $pattern from file $filename"
echo "The selected records are: "
grep "$pattern" $filename
echo "The no.of lines contains the word( $pattern ) :"
grep -c "$pattern" $filename
Output :
$ sh grep.sh
Enter the pattern to be searched:
computer
Enter the file to be used:
sales.dat
Searching for computer from file sales.dat
The selected records are:
10 computer 2345
10 computer 7685
The no.of lines contains the words ( computer ) :
A) Aim: write a shell script that takes a command –line argument and
reports on whether it is
directory, a file, or something else.
$ vi filetype.sh
echo "Enter the file name: "
read file
if [ -f $file ]
then
echo $file "---> It is a ORDINARY FILE."
elif [ -d $file ]
then
echo $file "---> It is a DIRCTORY."
else
echo $file "---> It is something else."
fi
outputs:
$ sh filetype.sh
Enter the file name:
sales.dat
sales.dat ---> It is a ORDINARY FILE.
$ sh filetype.sh
Enter the file name:
cse
cse---> It is a DIRCTORY.
$vi upper.sh
for file in *
do
if [ -f $file ]
then
echo $file | tr '[a-z]' '[A-Z]'
fi
done
Reading input :
$ ls
afile
afile1.tar
afile.tar
aped.sed
errfile
exch.dat
exsed.sed
f1
f2
for.sh
grep.sh
Output:
$ sh upper.sh
AFILE
AFILE.TAR
APED.SED
ERRFILE
EXCH.DAT
EXSED.SED
F1
F2
FOR.SH
GREP.SH
$vi logtime.sh
echo "Enter the USER NAME : "
read user
last $user
Output :
$ sh logtime.sh
Enter the USER NAME :
cse123
cse123 tty7 :0 Fri Sep 26 13:27 still logged in
cse123 pts/1 :0.0 Thu Sep 25 15:08 - 15:45 (00:37)
cse123 tty7 :0 Thu Sep 25 14:53 - 16:32 (01:39)
cse123 tty7 :0 Thu Sep 25 14:13 - 14:25 (00:11)
cse123 tty7 :0 Tue Sep 23 13:54 - 15:30 (01:36)
cse123 pts/2 :20.0 Mon Sep 22 17:02 - 17:23 (00:21)
A) Aim: 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.
$ vi range.sh
echo " Enter the file name :"
read file
if [ -f $file ]
then
echo "Enter the Starting line number:"
read snum
echo "Enter the Ending line number:"
read enum
if [ $snum -lt $enum ]
then
echo "The selected lines from $snum line to $enum line in $file :"
sed -n ' ' $snum','$enum' 'p' ' $file
else
echo "Enter proper starting & ending line numbers."
fi
else
echo "The file ' $file ' doesn't exists. "
fi
Output:
$sh range.sh
Enter the file name :
sales.dat
Enter the Starting line number:
2
Enter the Ending line number:
4
The selected lines from 2 line to 4 line in sales.dat :
11 tvsets 8765
10 computer 7685
12 mouse 6785
$ vi detline.sh
echo "Enter the word to search for all lines :"
read word
echo "the file name are $* ."
for i in $*
do
echo "The name of the file :" $i
grep -v $word $i
done
Output :
$ sh detline.sh sales.dat sales1.dat sales2.dat
Enter the word to search for all lines :
computer
the file name are sales.dat sales1.dat sales2.dat
The name of the file : sales.dat
11 tvsets 8765
12 mouse 6785
13 keyboard 2341
The name of the file : sales1.dat
11 tvsets 8765
12 mouse 6785
13 keyboard 2341
The name of the file : sales2.dat
11 tvsets 8765
12 mouse 6785
13 keyboard 2341
A) Aim: 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 the basic salary is>=1500 then HRA=500/- and DA=98% of the basic
Output :
$ sh gsalary.sh
enter the basic salary:
1200
The gross salary : 2400
$ sh gsalary.sh
enter the basic salary:
2400
the gross salary : 5252
$ vi pow.sh
echo "Enter the integer value :"
read int1
echo "Enter the power of that integer:"
read int2
pv=$int1
i=1
while [ $i -lt $int2 ]
do
pv=`expr $pv \* $int1`
i=`expr $i + 1 `
done
echo "The value of first number to the power of the second number
:"
echo "$pv"
output:
$ sh pow.sh
Enter the integer value :
2
Enter the power of that integer:
5
The value of first number to the power of the second number :
32
A) Aim: Write an interactive file –handling shell program. Let it offer the
user the choice of copying removing, renaming, or linking files. Once the
user has made a choice, have the same program ask the user for the
necessary information, such as the
file name ,new name and so on.
$ cat 8a.sh
echo "*******MENU*********"
echo "
1. List of files.
2. Copying files.
3. Removing files.
4. Renaming files.
5. Linking files."
Echo "enter your choice "
read ch
case "$ch" in
1 ) echo "The list of file names."
ls –l
2 ) echo "Enter the old filename."
read ofile
echo "Enter the new file name."
read nfile
cp $ofile $nfile && echo "Copied sucessfully." || echo
"Copied is not possible." ;;
3 ) echo "Enter the file name to remove."
read rfile
rm -f $rfile && echo "Successfully removed." ;;
Output :
$ sh 8a.sh
*******MENU*********
1. List of files.
2. Copying files.
3. Removing files.
4. Renaming files.
5. Linking files.
enter your choice
2
Enter the old filename.
f1
Enter the new file name.
f0
Copied sucessfully.
$vi login.sh
echo "Enter the USER NAME : "
read user
last $user
$ sh logtime.sh
Enter the USER NAME :
cse123
cse123 tty7 :0 Fri Sep 26 13:27 still logged in
cse123 pts/1 :0.0 Thu Sep 25 15:08 - 15:45 (00:37)
cse123 tty7 :0 Thu Sep 25 14:53 - 16:32 (01:39)
cse123 tty7 :0 Thu Sep 25 14:13 - 14:25 (00:11)
cse123 tty7 :0 Tue Sep 23 13:54 - 15:30 (01:36)
cse123 pts/2 :20.0 Mon Sep 22 17:02 - 17:23 (00:21)
(or)
C) Aim: 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.
$vi cheek.sh
echo "enter the first file name"
read file1
echo "enter the second file name"
read file2
cmp $file1 $file2 && rm $file2
if [ -e $file1 ]
Output:
$ sh cheek.sh
enter the first file name
file1.dat
enter the second file name
file3.dat
The two files contents are same.
The second file is deleted successfully.
$ sh 8c.sh
enter the first file name
file1.dat
enter the second file name
file2.dat
file1.dat file2.dat differ: byte 1, line 1
The two files contents are not same.
You cann't remove the file 'file2.dat' .
$ sh 8c.sh
enter the first file name
file5.dat
enter the second file name
file3.dat
cmp: file3.dat: No such file or directory
A) Aim: write a shell script that displays a list of all the files in the current
directory to which the use has read ,write and execute permissions.
$vi perm.sh
echo "The list of File Names in the curent directory."
echo "Which have Read,Write and Execute permisions. "
for file in *
do
if [ -f $file ]
then
if [ -r $file -a -w $file -a -x $file ]
then
ls -l $file
fi
fi
done
Output :
$ vi wcount.sh
echo " Enter the word to be searched"
read word
echo "Enter the filename to be used"
read flname
echo "the no. of times the word occured in the file."
grep -c $word $flname
Output:
$ sh wcount.sh
Enter the word to be searched
computers
Enter the filename to be used
sales.dat
the no. of times the word occured in the file.
2
$ vi strlen.sh
echo "To find the length of the given string."
echo "Enter the string."
read string
strlen=${#string}
echo "The string length is : $strlen"
Output :
$ sh strlen.sh
To find the length of the given string.
Enter the string.
computer science and engineering.
The string length is : 33
A) Aim: 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:
I)file type
#include<stdio.h>
#include<unistd.h>
#include<sys/stat.h>
#include<sys/types.h>
struct stat b;
int i;
for(i=1;i<argc;i++)
if(lstat(argv[i],&b)==-1)
printf("%s",argv[i]);
fflush(stdout);
perror("lstat error");
continue;
else if(S_ISREG(b.st_mode))
printf("regular file\n");
else if(S_ISDIR(b.st_mode))
else if(S_ISCHR(b.st_mode))
else if(S_ISBLK(b.st_mode))
else if(S_ISFIFO(b.st_mode))
printf("pipefile \n");
else if(S_ISLNK(b.st_mode))
else if(S_ISSOCK(b.st_mode))
else
printf("unknown file");
OUTPUT :
$./a.out lab1.sh
regular file
B) Aim: 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:
(i)number of links
(ii) read, write and execute permission
#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
int i;
if(argc<2)
printf("usage:./a.out< filename(s)");
exit(1);
for(i=1;argc!=i;i++)
if(access(argv[i],F_OK)<0)
continue;
else
if(lstat(argv[i],&statbuf)<0)
continue;
return(0);
OUTPUT :
No of links :1
#include<unistd.h>
#include<stdio.h>
#include<stdlib.h>
if(argc!=3)
exit(1);
if(access(argv[1],F_OK)<0)
exit(2);
if(rename(argv[1],argv[2])<0)
printf("Rename Error");
exit(3);
return 0;
OUTPUT :
#include<stdio.h>
FILE *fp1,*fp2;
int ch;
fp1=fopen(argv[1],”r”);
fp2=fopen(argv[2],”r”);
while((ch=fgtc(fp1))!=-1)
fputc(ch,fp2);
OUT PUT:
Cc filename c
La out ab abc
#include<stdio.h>
#include<conio.h>
#include<unistd.h>
#include<derent.h>
Int main()
{
DIR *dirp;
Struct dirent *dp;
Dirp=opendir(“”);
If(dirp==NULL)
Printf(“error”);
else
for(dp=readdir(dirp);dp!=NULL;dp=readdir(dirp))
printf(“%s\n”,dp->d=name);
}
Output:
cc filename c
la.output
a.out
ramesh
file1
file2
Descriptin:
To create a new directory we use the make directory command it has two
options
1) permission mode
2) parent mode
we can control the permission for the new directory with the mode
(-m) optin
Purpose:
To create a new directory we use the make directory command
Name: rmdir
Example: cd anu
Name: MAN
Example: man
$ man cal
NAME
Cal –display a calendar
Snopsis: cal display a simple calendar if arguments are not specified the
current month is displayed.
ps
name: ps command
syntax: ps
example: ps
PID TTY TIME CMD
659 tty3 00:00:01 sh
du
Name: du command
Example: du/home/dir1/dir2
11554/home/dir1/dir2/prg1
12820/home/dir1/dir2/prg2
Name: df
Syntax: df
Description: it is very much useful for finding the free disk space
Example: df
/ (dir1/dir2/file1):41224888 blocks
Name: head
Name: tail
Example: tali.abc
Cat abc
562
561
563
564
565
566
567
568
569
570
571
Head -4 abc
562
561
563
564
Tail -6
570
571
572
573
574
575
Tail -3 abc
573
574
575
Description: it compare the files line by line and display the results in three
column contains lines which are unique the center column contains unique
lines in file2 and the right column contains line found in both files.
Name:cmp
description: the action it takes depends on the option code when cmp
command is executed with out any options it stops at the first byte that is
different.
$ cat cmpfile1
123456
7890
$ cmp cmpfile1 cmpfile1.cpy
$ cat cmpfile2
123456
As9u
$cmp cmpfile1 cmpfile2
Redirecting input:
We can redirect the standard input from the keyboard to any text file the
input redirection operator is the less than characters (<).
Or
Command<file1
Redirecting output
example:
1. who>whooct2
ksh:whooct2:file already exists
1. who >|whooct2
$ more whooct2
Abc 52408 ttyq3 oct 2 15:24 (atc2west -17/atc.fhda)
Example: input
This is raining today
Today is very hot
God morning
Output:
Input:
Raj 1234 abc
Ram 7892 xyz
Ram 7782 ykm
Output:
Ram 7892 xyz
Ram 7782 ykm
Output:
Output:
Output:
There are no blank lines.
Input:
22 78 44
66 31 70
52 30 44
88 31 66
Output:
Print totals
22+78+44=144
66+31+70=167
52+30+44=126
88+31+66=185
End totals
Output: