330 likes | 514 Views
The Environment. 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.
E N D
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.) X=10 . . . . login shell (parent shell) . . . X=5 . . . . varfile (subshell) . . .
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.) exported variables local variables y=10 X=100 login shell . . . . . . copied exported variables local variables y=10 vartest3 (subshell) . . . . . .
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 X=100 exported variables local variables login shell . . . . . . copied y=10 x=50 y=5 exported variables local variables vartest4 (subshell) . . . . . .
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 local variables x=100 login shell . . . . . . y=10 z=10 x=50 y=5 exported variables local variables vartest4 (subshell) . . . . . . local variables exported variables y=10 z=1 vartest5 (subshell) . . .
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.) exported variables local variables x=100 y=10 login shell . . . . . . local variables exported variables x=50 y=5 z=1 vartest4 (subshell) . . . . . . local variables exported variables y=5 z=1 vartest5 (subshell) . . .
Local and Exported variables Summary • 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.) • 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 $
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 Documents monitor personal varfile3 cdfile greetings~ myln rem vars $
{ . . .; } 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/etc/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.) # Login and -su shells get /etc/profile services. trap "" 2 /bin/mail -E case $? in 0) echo "You have new mail." ;; 2) echo "You have mail." ;; esac fi esac umask 022 trap 2 3
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