Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1

2 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

3 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

4 Subshell (continue.) X=10. login shell (parent shell) X=5. varfile (subshell)...

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 =

6 Exporting variables (continue.) login shell vartest3 (subshell) y=10... X=100... y=10... exported variables exported variables local variables local variables copied

7 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

8 Exporting variables (continue.) login shell vartest4 (subshell) y=10... X=100... x=50 y=5... y=10... exported variables exported variables local variables local variables copied

9 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

10 Exporting variables (continue.) $ varfile4 x = y = 10 z = 1 $

11 Exporting variables (continue.) login shell vartest4 (subshell) y=10... x=100... x=50 y=5... y=10 z=10... vartest5 (subshell) y=10 z=1... exported variables exported variables local variables local variables exported variables local variables

12 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

13 Exporting variables (continue.) login shell vartest4 (subshell) y=10... x=100... x=50... y=5 z=1... vartest5 (subshell) y=5 z=1... exported variables exported variables local variables local variables exported variables local variables

14 Local and Exported variables Summary 1. Any variable that is not exported is a local variable whose existence will not be known to subshells 2. 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 3. 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

15 Local and Exported variables Summary (continue.) 4. 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 5. A variable can be exported any time before or after it is assigned a value

16 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

17 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 $

18 Variables used by the shell VariableMeaning CDPATHThe directories to be searched whenever cd is executed without a full path as argument HOMEThe directory that cd changes to when no argument is supplied IFSThe 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

19 Variables used by the shell (continue.) VariableMeaning MAILThe 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. MAILCHEC K 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.

20 Variables used by the shell (continue.) VariableMeaning MAILPATHA 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) PATHA 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) PS1The primary command prompt, normally "$ " PS2The secondary command prompt, normally ">"

21 Variables used by the shell (continue.) Variable Meaning SHACCTThe 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 SHELLThe 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

22 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 $

23 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

24 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 $

25 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

26 Shell Script: wcl Count the number of lines in a file To view the source code of wcl click herehere $ wcl /etc/passwd 15

27 (...) 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 $

28 {...; } 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

29 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)

30 .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 $

31 /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

32 /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

33 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


Download ppt "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."

Similar presentations


Ads by Google