CM 304

You might also like

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 31

DEPARTMENT OF TECHNICAL EDUCATION

ANDHRA PRADESH
Name : A V N L Sarojini
Designation : Lecturer
Branch : Computer Engineering
Institute : A.A.N.M. & V.V.R.S.R. Poly.,
Gudlavalleru.
Year/Semester :III Semester
Subject : UNIX & C
Subject Code : CM-304
Topic : Shell Programming &
Filtering Techniques
Duration : 50 Min
Sub Topic : Shell built-in commands
Teaching Aids :PPT
CM304.18 1
Objective

On completion of this period, you will be able to learn


 Shell built-in commands
– export command
– set command
– shift command
– readonly command

CM304.18 2
Recap

 UNIX provides two types of variables, the system


variables and local variables.

 UNIX allows to group multiple commands into one


line using the semicolon operator(;).

CM304.18 3
Shell built-in commands

 UNIX provides export, set, shift and readonly


commands to work on shell variables.

CM304.18 4
export command

 When the new program starts execution it creates a


process and inherits some of the environment
variables that are available in the parent process.

 By default the values stored in the shell variables are


local to the shell.

CM304.18 5
export command

 The local variables are available only in the shell in


which they are defined, they are not passed through
the child process.

CM304.18 6
export command

 The shell offers a facility of exporting local


variables to all child processes, so that once
defined they are available globally. This is done
by using export command

CM304.18 7
export command
Ex:
$cat >t.sh
echo the value of x is $x
x=20
echo the value of x is $x
$x=10
$export x
X is made Global
$sh t.sh variable here
the value of x is 10
the value of x is 20
$echo $x
20 CM304.18 8
readonly command (or) unchanging variable

 In some situations need may arise for variable


to have a constant (or) fixed value. We can
achieve this by using readonly statement.

Ex:
$a=20
$readonly a

CM304.18 9
readonly command (or) unchanging variable

 When the variable are made readonly the shell does


not allow us to change their values.

 All such readonly variables can be listed by entering


readonly at the ‘$ prompt’.

CM304.18 10
Positional Parameters

 On many occasions we may need to convey


information to a program.

 A very convenient way of doing this is by specifying


arguments at the command line.

Example: $sh test.sh 05030cm02 Kumar

CM304.18 11
Positional Parameters

 To know what has been passed to the program the shell


uses positional parameters.

 This can be thought of as variables defined by shell.

 They are nine in number, named $1, $2, $3 through $9

CM304.18 12
Positional Parameters

Example:

$sh test.sh 05030cm02 kumar

Then
$1 is assigned to 05030cm02

$2 is assigned to kumar

CM304.18 13
Positional Parameters

 Example program to copy files

$cat copy.sh
cp $1 $2
cat $2
$sh copy.sh abc xyz

$1 is assigned with abc , $2 is assigned with xyz, then


abc file is copied to xyz
CM304.18 14
Positional Parameters

 Example program to change permissions of a file.

$cat xyz.sh
chmod 744 $1
$sh xyz.sh abc

CM304.18 15
set command (setting values to positional parameters)

 Setting of positional parameters as shown below is not


allowed.
Example:
$1=10
$2=30

CM304.18 16
set command

 There is one way to assign values to the positional


parameters that is by using set command.

CM304.18 17
set command

Example:

$set hai hello welcome to UNIX

The above command sets the value of


$1 with “hai”
$2 with “hello”
$3 with “welcome”
$4 with “to”
$5 with “unix”
CM304.18 18
set command

Example : Write a shell script to display your own login


name.

$cat log.sh
set `who am i`
echo $1
$sh log.sh

CM304.18 19
set command

 Write a shell program to display system date in the


below format
FRI 19 APR 2006

$cat mdate.sh
set `date`
echo $1 $3 $2 $6
$sh mdate.sh
CM304.18 20
shift command

 It is know that there are only 9 positional parameters.

 In case more than 9 positional parameters are given in


a command line, no error is indicated.

 To reach the remaining values we must use the shift


command

CM304.18 21
shift command

 When, used the shift statement shifts out the values


assigned to positional parameters to the left by an
integer value mentioned with the shift statement.

Example : $shift 5

Moves the parameter values to the left by 5 positions.

CM304.18 22
shift command
Example:
$set Everyone has the capacity to learn from mistakes. He learns
a lot from experience.

$echo $1 $2 $3 $4 $5 $6 $7 $8 $9
Everyone has the capacity to learn from mistakes.

$shift 5
$echo $1 $2 $3 $4 $5 $6 $7 $8 $9
learn from mistakes. He learns a lot from experience.

CM304.18 23
shift command

 When used with no arguments shift command shifts


the contents of positional parameters by just one
position to the left.

CM304.18 24
Summary

 UNIX provides export, set, shift and readonly commands to work


on shell variables.

 export to make local variables as global variables.

 Readonly to unchange variables.

 Set to set values to positional parameters.

 Shift to shift values of positional parameters.

CM304.18 25
Quiz

1.Can we write the following statement in shell program


[Yes/No]

$3=“hello”

CM304.18 26
Quiz

1.Can we write the following statement in shell program


[Yes/No]

$3=“hello”

CM304.18 27
Quiz

2.If more than 9 positional parameters are given in a


command line, the shell display an error [YES/NO]

CM304.18 28
Quiz

2.If more than 9 positional parameters are given in a


command line, the shell display an error [YES/NO]

CM304.18 29
Assignment

 Write a shell script which will receive the filename.


This script should obtain information about this file
as given by ls -l and display it in proper format.

CM304.18 30
Frequently asked Questions

1.List and explain the shell built-in commands

CM304.18 31

You might also like