33

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

Embed Size (px)

Citation preview

Page 1: 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
Page 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

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

Page 3: 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

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 varfilex=5echo :$x:$ x=10$ varfile:5:$ echo $x10

Page 4: 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

Subshell (continue.)

X=10....

login shell(parent shell)

X=5....

varfile(subshell)

. . .

. . .

Page 5: 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

Exporting variables

Format: export variables Make the value of a variable known to a subshell

$ cat varfile2echo x = $xecho y = $y$ x=100$ y=10$ varfile2x =y =$ export x$ varfile2x = 100y =

Page 6: 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

Exporting variables (continue.)

login shell

vartest3(subshell)

y=10

. . .

X=100

. . .

. . .

y=10

. . .

exported variables

exported variables

localvariables

localvariables

copied

Page 7: 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

Exporting variables (continue.)

There is no way to change the value of a variable in a parent shell from within a subshell

$ cat varfile3x=500y=5 $ varfile3$ echo $x $y50 5

Page 8: 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

Exporting variables (continue.)

login shell

vartest4(subshell)

y=10

. . .

X=100

. . .

x=50y=5

. . .

y=10

. . .

exported variables

exported variables

localvariables

localvariables

copied

Page 9: 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

Exporting variables (continue.)

Once a variable is exported, it remains exported to all subshells that are subsequently executed

$ cat varfile4x=50y=5z=1export zvartest5$ cat vartest5echo x = $xecho y = $yecho z = $z

Page 10: 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

Exporting variables (continue.)

$ varfile4x = y = 10z = 1$

Page 11: 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

Exporting variables (continue.)

login shell

vartest4(subshell)

y=10

. . .

x=100

. . .x=50y=5

. . .

y=10z=10

. . .vartest5

(subshell)

y=10z=1

. . .

exported variables

exported variables

localvariables

localvariables

exported variables

localvariables

Page 12: 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

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 varfile6x=50y=5z=1export y z$ varfile6

Page 13: 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

Exporting variables (continue.)

login shell

vartest4(subshell)

y=10

. . .

x=100

. . .x=50

. . .

y=5z=1

. . .vartest5

(subshell)y=5z=1

. . .

exported variables

exported variables

localvariables

localvariables

exported variables

localvariables

Page 14: 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

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

Page 15: 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

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

Page 16: 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

export with no arguments

export with no arguments prints a list of the variables that are explicitly exported by the current shell

$ exportexport xexport z

Page 17: 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

Current Directory

There no way to change the current directory of a parent shell from a subshell

$ cat cdfilecd /usr/bin/pwd$ pwd/home/sbenayed/UNIX$ cdfile/usr/bin$

Page 18: 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

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

Page 19: 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

Variables used by the shell (continue.)

Variable MeaningMAIL 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.

MAILCHECK

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.

Page 20: 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

Variables used by the shell (continue.)

Variable MeaningMAILPATH 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 ">"

Page 21: 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

Variables used by the shell (continue.)

Variable MeaningSHACCT 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

Page 22: 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 . Command

Format: . File dot command executes the contents of file in the current

directory

$ cat varsUNIX=/home/abuzneid/UNIX$ vars$ echo $UNIX

$ . vars$ echo $UNIX/home/abuzneid/UNIX$

Page 23: 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 exec Command

Format: exec program exec command replaces the current program with

the new one Startup time of an exec'ed program is quicker

Page 24: 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

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; done123$ echo $x3 $ for y in 1 2 3; do echo $y; done > /tmp/foo $ echo $y

$

Page 25: 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

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

Page 26: 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

Shell Script: wcl

Count the number of lines in a file To view the source code of wcl click here

$ wcl /etc/passwd15

Page 27: 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

(. . .) 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 varfile3cdfile greetings~ myln rem vars$

Page 28: 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

{ . . .; } 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 $x10$ { x=100; }$ echo $x100

Page 29: 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

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)

Page 30: 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

.profile File

The login shell executes two special files on the system /etc/profile .profile in the home directory

$ cat $HOME/.profilestty decPATH=/bin:/usr/bin:/usr/ucb:/etc:/usr/etc:/usr/etc/install:.export PATH TERMecho This is .profileumask 077$

Page 31: 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

/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 3export LOGNAME PATH if [ "$TERM" = "" ]then if /bin/i386 then TERM=sun-color else TERM=sun fi export TERMfi

Page 32: 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

/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 fiesac umask 022trap 2 3

Page 33: 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

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