How to run a shell program

Download Report

Transcript How to run a shell program

Environment



After log in into the system, a copy of the shell is given to
the user
Shell maintains an environment which is distinct from one
user to another
The environment is maintained all the time until the user
logs off
Subshell



Subshell is a new shell that is executed by the login shell in order to
run a desired program
A subshell has no knowledge of local variables that were assigned
values by the parent shell
The subshell cannot change the value of a variable in the parent
shell
$ cat varfile
x=5
echo :$x:
$ x=10
$ varfile
:5:
$ echo $x
10
Subshell (continue.)
login shell
(parent shell)
X=10
.
.
.
.
...
varfile
(subshell)
X=5
.
.
.
.
...
Exporting variables


Format: export variables
Make the value of a variable known to a subshell
$ cat varfile2
echo x = $x
echo y = $y
$ x=100
$ y=10
$ varfile2
x =
y =
$ export x
$ varfile2
x = 100
y =
Exporting variables (continue.)
y=10
exported
variables
login
shell
...
y=10
...
local
variables
...
copied
exported
variables
X=100
vartest3
(subshell)
local
variables
...
Exporting variables (continue.)

There is no way to change the value of a variable
in a parent shell from within a subshell
$ cat varfile3
x=500
y=5
$ varfile3
$ echo $x $y
50 5
Exporting variables (continue.)
y=10
exported
variables
...
login
shell
local
variables
X=100
...
copied
y=10
...
exported
variables
vartest4
(subshell)
local
variables
x=50
y=5
...
Exporting variables (continue.)

Once a variable is exported, it remains exported to
all subshells that are subsequently executed
$ cat varfile4
x=50
y=5
z=1
export z
vartest5
$ cat vartest5
echo x = $x
echo y = $y
echo z = $z
Exporting variables (continue.)
$ varfile4
x =
y = 10
z = 1
$
Exporting variables (continue.)
y=10
exported
variables
login
shell
local
variables
...
...
y=10
z=10
exported
variables
vartest4
(subshell)
...
y=10
z=1
exported
variables
vartest5
(subshell)
x=100
local
variables
local
variables
x=50
y=5
...
...
Exporting variables (continue.)

If a variable gets exported again in a subshell, the
changed value of the variable will get passed
down to all subshells (from the subshell that
rexported the variable)
$ cat varfile6
x=50
y=5
z=1
export y z
$ varfile6
Exporting variables (continue.)
y=10
exported
variables
login
shell
...
y=5
z=1
...
y=5
z=1
exported
variables
exported
variables
local
variables
local
variables
vartest4
(subshell)
vartest5
(subshell)
x=100
...
x=50
...
local
variables
...
Local and Exported variables
Summary
1.
2.
3.
Any variable that is not exported is a local variable whose
existence will not be known to subshells
Exported variables and their values are copied into a subshell's
environment, where they may be accessed and changed.
However, such changes have no affect on the variables in the
parent shell
If a subshell explicitly exports a variable, then changes made to
that variable affect the exported one. If a subshell does not
explicitly export a variable, then changes made to that variable
affect a local one, even if the variable was exported from a
parent shell
Local and Exported variables
Summary (continue.)
4.
5.
Exported variables retain this characteristic not
only for directly spawned subshells, but also for
subshells that affect the exported one. If a
subshell affect a local one, even if the variable
was exported from a parent subshell
A variable can be exported any time before or
after it is assigned a value
export with no arguments

export with no arguments prints a list of the variables
that are explicitly exported by the current shell
$ export
export x
export z
Current Directory

There no way to change the current directory of
a parent shell from a subshell
$ cat cdfile
cd /usr/bin/
pwd
$ pwd
/home/sbenayed/UNIX
$ cdfile
/usr/bin
$
Variables used by the shell
Variable
Meaning
CDPATH
The directories to be searched whenever cd is executed
without a full path as argument
HOME
The directory that cd changes to when no argument is
supplied
IFS
The Internal Field Separator characters; used by the shell
to delimit words when parsing the command line, for the
read and set commands, when substituting the output from
a back-quoted command, and when performing parameter
substitution. Normally, it contains the three characters
space, horizontal tab, and newline
Variables used by the shell (continue.)
Variable
Meaning
MAIL
The name of a file that the shell will periodically
check for the arrival of mail. If new mail arrives,
then the shell will display its You have mail
message.
The number of seconds specifying how often the
shell is to check for arrival of mail in the file
MAIL or in the files listed in MAILPATH. The
default is 600. A values of 0 causes the shell to
check before displaying each command prompt.
MAILCHEC
K
Variables used by the shell (continue.)
Variable
Meaning
MAILPATH
A list of files to be checked for the arrival of mail.
Each file is delimited by a colon, and can be followed
by a percent sign (%) and a message to be displayed
when mail arrives in the indicated file (You have mail
is the default)
PATH
A colon delimited list of directories to be searched
when the shell needs to find a command to be
executed. The current directory is specified as :: (if it
heads the list, : suffices)
PS1
The primary command prompt, normally "$ "
PS2
The secondary command prompt, normally ">"
Variables used by the shell (continue.)
Variable
Meaning
SHACCT
The name of a file that the shell will use to write
accounting information for commands that it executes.
This information can later be analyzed using the
acctcom command
SHELL
The name of the shell (e.g. bin/sh). This variable is
used by vi and ed to determine the shell to startup
when you escape to the shell or execute a shell
command. It's also used by the shell on startup to
determine if it should run restricted. This
determination is made based upon whether the letter
"r" appears in the name of the shell
The . Command


Format: . File
dot command executes the contents of file in the current
directory
$ cat vars
UNIX=/home/abuzneid/UNIX
$ vars
$ echo $UNIX
$ . vars
$ echo $UNIX
/home/abuzneid/UNIX
$
The exec Command



Format: exec program
exec command replaces the current program with
the new one
Startup time of an exec'ed program is quicker
I/O Redirection and Subshells

shell executes in a subshell commands like if, for, while
and until if their input and/or output is redirected or piped
$ for x in 1 2 3; do echo $x; done
1
2
3
$ echo $x
3
$ for y in 1 2 3; do echo $y; done > /tmp/foo
$ echo $y
$
Change Standard I/O to a file



To change the standard input to a file: exec < file
To change the standard output to file: exec > file
To reassign standard input back to terminal:
exec < dev/tty

To reassign standard input back to terminal:
exec < dev/tty
Shell Script: wcl


Count the number of lines in a file
To view the source code of wcl click here
$ wcl /etc/passwd
15
(. . .) construct



Groups a set of commands to set them to be
executed by a subshell
Input and output can be piped to and from this
construct, and I/O can be redirected
Used to send a set of command to the background
$ (cd UNIX; ls)
addi
personal
cdfile
rem
$
Documents
varfile3
greetings~
vars
monitor
myln
{ . . .; } construct



Groups a set of commands to set them to be executed by
the current shell
Input and output can be piped to and from this construct,
and I/O can be redirected
A space must follow the left hand brace, and a semicolon
must appear after the last command
$ x=10
$ (x=100)
$ echo $x
10
$ { x=100; }
$ echo $x
100
Another Way to Pass variables to a
Subshell

Proceed name of the command with the
assignment of as many variables like:
var1=value1 var2=value program

is identical to:
(var1=value1; var2=value2; export var1 var2; program)
.profile File

The login shell executes two special files on
the system


/etc/profile
.profile in the home directory
$ cat $HOME/.profile
stty dec
PATH=/bin:/usr/bin:/usr/ucb:/etc:/usr/etc:/usr/et
c/install:.
export PATH TERM
echo This is .profile
umask 077
$
/etc/profile File
$cat /etc/profile
#ident "@(#)profile 1.18
98/10/03 SMI" /*SVr4.0 1.3 */
# The profile that all logins get before using their own
.profile.
trap "" 2 3
export LOGNAME PATH
if [ "$TERM" = "" ]
then
if /bin/i386
then
TERM=sun-color
else
TERM=sun
fi
export TERM
fi
/etc/profile File (continue.)
#
esac
Login and -su shells get /etc/profile services.
trap "" 2
fi
umask 022
trap 2 3
/bin/mail -E
case $? in
0)
echo "You have new mail."
;;
2)
echo "You have mail."
;;
esac
References



UNIX SHELLS BY EXAMPLE BY ELLIE
QUIGLEY
UNIX FOR PROGRAMMERS AND USERS
BY G. GLASS AND K ABLES
UNIX SHELL PROGRAMMING BY S.
KOCHAN AND P. WOOD