Beruflich Dokumente
Kultur Dokumente
It includes the entire gamut of unix command command with syntax of programming
language called shell programming.
Shell programming perspective
Shell forms an interface between user and the system.
Incorporates a powerful programming language that enables the user to exploit
the full power and versatility of unix.
File that contains collection of valid unix commands along with the
construct of the programming language is called shell script or shell
programming.
you
Translate result s back to you
Bourne shell
Bash shell
Korn shell
C shell
Dept. of M.C.A.
Page 1 of 18
Description:Shell
Bourne
Creator
Steave Bourne of AT & T Bell.
Prompt
$
Bash
Korn
David Korn
C shell
california.
$who<enter>
$ls ?<enter>
3) IO REDIRECTION
4) PIPE LINE MECHANISM
5) ENVIRONMENT CONTROL
6) INTERPRETING PROGRAMMING LANGUAGE
FOR KNOWING THE CURRENT WORKING SHELL:$echo $SHELL<ENTER>
for bourne shell- /bin/sh or /bin/bsh
for korn shell- /bin/ksh
for bash shell- /bin/bash
to change the shell:$echo $SHELL<enter>
/bin/sh
$ksh<enter>
$echo $SHELL<enter>
/bin/ksh
$^d
Dept. of M.C.A.
Page 2 of 18
$vi test
compilation + execution
$ sh<file name>
no object file is created .Shell script need no extention(not mendatory)
another way to execute is changemod ,give execution permission
and then execute.
$ chmod 700
$ test
sh :-execution of the shell script is done with the help of sh command as a newly
borned script you have only read and write permission turn on where as execute
permission is turn off..thus shell script can be invoked in either of the two ways:1) invoking the sh command with file name as argument .
2) making the shell script executable by changing its excess permission and then
invoking the file.
Unix system variables:These variables are a part of unix system and can not be altered .
1) PS1:- stands for system prompt 1.
$ echo $ PS1<enter>
$
$ PS1 = #<enter>
#
2) PS2:- system prompt 2 that is secondary prompt
$ echo $ PS2<enter>
>
$
3) PATH:- defines the path which the shell must search in order to execute any
command or file
$ echo PATH<enter>.
Dept. of M.C.A.
Page 3 of 18
/usr/local/bin:/usr/bin:/bin:/usr/bin/x11:/usr/games.
4) HOME:- store the default working directory of the user.
$ echo $ HOME<enter>
/*assume that your register number is your login name*/
/usr/ 1pi02mca09
5) LOGNAME:- stores the login name of the user
$ echo $ LOGNAME<enter>
ngp
$
6) MAIL:- defines the file along with the path where the name of the user is stored
/usr/MAIL/ngp
7) MAILCHECK :- defines the duration after which yhe shell checks wether the user
has recieved any mail
$ echo $ MAILCHECK<enter>
60
$
8) SHELL:- defines the name of your working shell.
9) TERM :- defines the name of the terminal on which you are working
$ echo $ TERM<enter>
ansi
$
10)TZ:- defines the name of the time zone on which you are working .
Dept. of M.C.A.
Page 4 of 18
The variables can constitute alphabets,digits and underscores .However the first
character must be either an alphabet or an underscore
Example:$ a=10<enter>
$ echo $a<enter>
10
$
tips and traps:1)All shell variables are string variables .
Example:- in A=10,where 10 is stored in A ,is not treated as a number but as a
string
operations.
2) Variables may contain more than one word in which case it should be enclosed in
Example:$a = Hello world <enter>
$echo $ a
Hello world
$
3) We can carry out more than 1 assignment in a single line.
Example:$name = abc age = 5<enter>
$echo $name $age<enter>
abc 5
$
4) A variable may not be given any value.They are called as NULL variables.A NULL
variable can be treated in any 1 of the follwing ways:$d =
OR
$d =
OR
$d
5) I/O operations in the shell:-
Dept. of M.C.A.
Page 5 of 18
The output operation is performed with the help of echo statement,whereas the
input is
performed with the help of read statement.
Question:Write a shell script to accept a number and display.
Answer:echo enter the numbers
read num
echo u entered $num
VARIABLES:3 basic type of variables available:1) POSITIONAL PARAMETERS:- invoked by normal shell variables.It is called as
positional parameter or postional variables depending upon the position it occupies
during the execution stage.
EXAMPLE:echo my name is $1
echo your name is $2
$sh test abc xyz
Dept. of M.C.A.
Page 6 of 18
Dept. of M.C.A.
Page 7 of 18
: addition
: subtraction
: multiplication
: division
Meaning
Equal to
Not equal to
Greater than
Greater than or equal to
Less than
Less than or equal to
QUESTION :Write a shell script to accept a number and check whether it is greater than 10 or not
Dept. of M.C.A.
Page 8 of 18
Dept. of M.C.A.
Page 9 of 18
It is concerned with the existence of the file and its property.The following
operators are used.
Operators
-s
-f
-d
-w
-r
-x
QUESTION :-
Meaning
Check that file exists & not empty
Check that file is an ordinary file
Check whether file is directory
Check that file is writeable
Check that file is readable
Check that file is executable
Write a shell script to accept a file name and check whether it is executable or not,if
not make it executable
echo enter the filename
read fname
if [-x $fname]
then
echo file $fname is executable
else
chmod 700 $fname
fi
SAMPLE OUTPUT :$sh test
enter the filename
prg1
$ls l prg1
-rwx
a shell script which will accept a filename and starting and ending line
Dept. of M.C.A.
Page 10 of 18
2. Two numbers are entered from keyboard. Write a script to find the value of one
number raised to power of another.
3. Write
a shell script which deletes all lines containing the word unix in the files
supplied as arguments.
4. Write a shell script which displays a list of all the files in the current directory to
which you have read, write and execute permissions.
5. A shell script receives even number of filenames as arguments. Suppose four files
are supplied as an arguments, then the first file should get copied into second, third
file into fourth and so on. If odd number of filenames are supplied then no copying
should take place and an error message should be displayed.
6. Write a shell script which will receive any number of filenames as arguments. The
shell script should check whether every argument supplied is a file or a directory. If
it a directory it should be appropriately reported. If it is a filename then name of
the file as well as the number of lines present in it should be reported.
7. Write
a shell script which gets executed the moment the user logs in. It should
Dept. of M.C.A.
Page 11 of 18
receive a list of filenames, the first of which would be wordfile. The shell script
should report all occurrences of each word in wordfile in the rest of the files
supplied as arguments.
14. The word unix is present in only some of the files supplied as arguments to this
shell script. Your shell script should search each of these files in turn and stop at
the first file that it encounters containing the word unix. The filename should be
displayed on the screen.
15. Write a shell script which will receive any number of filenames as arguments. The
shell script should check whether such files already exists. If they do, then they
should be reported. If any of these files do not exist then check if a sub directory
called mydir exists in the current directory. If it doesnt exist then it should be
created and in it the files supplied as an arguments should be created. If mydir
already exists then it should be reported along with the number of files that are
currently present in mydir.
16. Write a shell script to determine how long a user has been working on the system.
17. Write a shell script which accepts any number of arguments and prints them in
reverse order. Ex : If file name is test then $ sh test A B C should produce C B A.
18. Write a shell script that accepts a path name and creates all the components in that
path name as directories. For example if the file name is test then : $ sh test
a/b/c/d should create directories a, a/b, a/b/c, a/b/c/d.
19. Write a shell script to implement terminal locking ( similar to lock command ), it
should prompt for a password. After accepting the password entered by the user, it
must prompt again for the matching password as confirmation and if the match
occurs, it must lock the keyboard, until a matching password is entered again. Your
script should take care of disregarding break, control-d etc. No time limit need be
implemented for the lock duration.
20. Write a shell script that finds and displays all the links to a file specified as the first
argument to the script. The second argument which is optional can be used to
specify the directory in which the search to begin. If this second argument is not
present the search is to begin in the current directory. In either case the starting
directory as well as all its sub directories at all levels must be searched.
21. Write a shell script to display the calendar for current month with current date
replaced by * or ** depending on whether the date is single digit or two digits.
Dept. of M.C.A.
Page 12 of 18
Shell scripts:1. write a shell script to accept a number and check wether it is greater than 10 or
not.
ANS:
echo Enter the number
read num
if [$ num -gt 10]
then
echo number is greater than 10
else
echo numbe is less than 10
fi
2. write a shell script to find the number of users currently working.
ANS:
User=`who|wc-l`
If [$ user eq 1]
then
echo u are the only user
else
echo $user user are working
3. write a shell script to accept a filename and check wether it is executable or not
if not make executable.
ANS:
echo enter the filename
read fname
if [-x $fname]
then
echo file $fname is executable
else
chmod 700 $fname
fi
4. write a script using while loop to print from 1 to 10.
Dept. of M.C.A.
Page 13 of 18
ANS:
While[$num-le 10]
do
echo $num
i=`expr $i + 1`
done
5. write a shell script to check wether ur current working directory is same as
home directory.
ANS:
pwd1=`pwd`
if [`$pwd1=$HOME`]
then
echo you r working in ur home directory
else
echo you are not in ur home directory
6. create a unix command menu using case.
ANS:
echo unix command menu
echo 1: list of files
echo 2: long list of files
echo 3: current working directory
echo 4: list of users
echo enter your choice
read choice
case $choice in
1) ls;;
2) ls-l;;
3) pwd;;
4) who;;
7. write a shell script which will accept any number of filename as argumants. The
script should check wether every argument supplied is either a file of directory.
Dept. of M.C.A.
Page 14 of 18
Dept. of M.C.A.
Page 15 of 18
10. write a schell script to find out wether the user whose login name passed as
parameter is currently logged in or not. If the user has logged in send the file
Hello available in your pwd to his terminal.
ANS:
Clear
If who|grep $1
then
terminal = `who | grep $1 | tr-s : | cut-d : -f2
cat hello > /dev/$terminal
11. write a shell script to reverse a string
ANS:
Clear
len = `expr length $str`
count=1
while [$len ge $count]
do
char = `echo $1 | cut c $len
rev = $rev$char
len = `expr len-1`
done
echo the reversr string is $rev
12. write a shell script that accepts two filename as arguments. Check if the
permissions of these are identical and if they are identical, output permission
otherwise o/p each filename with its permission.
ANS:
file1p = `ls-l $1| tr s : | cut c2-4`
file2p = `ls-l $2| tr s : | cut c2-4`
if [$file1p = $file2p]
then
echo common permissions $file1p
else
echo file $1 has $file1p permission
echo file $2 has $file2p permission
Dept. of M.C.A.
Page 16 of 18
13. write a shell script which reports name and size of the files in the directory
[dirctory name specified as argument] whose size exceeds 1000 bytes.
Filenames should be printed in descending order of their sizes. Total number of
files should be reported.
ANS:
Clear
if [$# -ne 1]
then
echo Improper term
exit
fi
if [! d$1]
then
echo $1 is not a directory
exit
fi
cd $1 rm temp
for i in *
do
size = `ls-l $i | tr-s : | cut d : f5`
if [$ size gt 1000]
then
fname = `ls-l $i | tr-s : | cut d : f10`
echo $fname : $size >>temp
fi
done
sort-t : +1nr temp
echo total number of files is `cat emp | wc-l`
exit
14. write a shell script called WHERE AM i when invoked, it should point out the
users name. The host name of the current machine, full path, name of the
Dept. of M.C.A.
Page 17 of 18
current working directory, current date and lists the contents of current
directory.
ANS:
echo the username is `who am i`
echo the host name is `uname`
echo the current workin directory `PATH`
echo the current data is `date`
echo the files in the directory is `ls`
Dept. of M.C.A.
Page 18 of 18