420 likes | 585 Views
More Variables. Parameter substitution. Parameter substitution (continue.). The $0 variable. Shell stores the name of the program inside the special variable $0. $ cat foo $0 is running!! $ foo foo: cannot fork: no swap space $. The set Command. The set Command (continue.).
E N D
The $0 variable • Shell stores the name of the program inside the special variable $0 $ cat foo $0 is running!! $ foo foo: cannot fork: no swap space $
The –x option • Turns on trace mode in the current shell • Traced commands are preceded by plus (+) sign, but variable assignments are not • Subshells can be traced by running the shell with the –x option followed by the name of the program to be executed sh -x program • To turn off trace mode: set +x • Any number of set -x and set +x can be inserted inside the program to turn trace on and off as desired
The –x option (continue.) $ x=* $ set -x set command trace option $ echo $* + echo set command trace option set command trace option $ mysl=ls mysl=ls $ ls | wc -l + ls + wc -l 32 $
set with no Arguments • set with no arguments gives an alphabetized list of all the variables whether they are local or exported $ set . . . WD=/home/sbenayed SHELL=/bin/csh TCAT=/usr/lib/lp/postscript/dpost TERM=vt100 TZ=US/Eastern UNIX=/home/abuzneid/UNIX USER=sbenayed mysl=ls x=*
Using set to reassign Positional Parameters $ set a b c d + set a b c d $ echo $1:$2:$3:$4 + echo a:b:c:d a:b:c:d $ echo $# + echo 4 4 $ echo $* + echo a b c d a b c d $ for arg; do echo $arg; done syntax error: `;' unexpected $
Using set to reassign Positional Parameters (continue.) # # Count words on a line # read line set $line echo $# $ parse + parse I love New York city 6 $
The -- option • If the arguments passed to set has (-) character, it will connect it as an option • If there is white space arguments, a list of the variables in the current shell will be displayed • If – option is used • set will interpret any subsequent arguments as option • prevents set from displaying all variables in the current shell
Shell Program: parse • Counts all the words on standard input • To view the source code of parse click here $ parse < $HOME/.profile 2 $ wc -w < $HOME/.profile 12 $
The IFS Variable • Internal Field Separator • Shell uses IFS when • passing input from the read command • output from command substitution (back-quotating) • performing variable substitution • The actual characters that are stored in IFS where 040 is whit espace, 011 is tab character and 012 the new line character (the second 012 comes from echo) $ echo "$IFS" | od -b 0000000 040 011 012 012 0000004
The IFS Variable (continue.) • IFS can be changed to any character $ read line This is a line $ echo "$line" This is a line $ IFS=" > " $ read line set it to just a line This is a line $ echo "$line" This is a line leading space preserved $
The IFS Variable (continue.) • Shell doesn't use the IFS when performing variable assignment • Changing the IFS is often done in conjunction with execution of the set command $ var=x:y:z $ echo "$var" x:y:z $ line="Micro Logic Corp.:BOX 174:Hachensack, NJ 07602" $ IFS=: $ set $line $ echo $# 3 $ for field; do echo $field; done syntax error: `;' unexpected
The readonly Command • Specify variables when values cannot be subsequently changed • To get a list of readonly variables • readonly variables attribute is not passed down to subshells $ MYPATH=/usr/bin:: $ readonly MYPATH $ MYPATH=/usr/local/bin:: MYPATH: is read only $ $ readonly readonly MYPATH
The readonly Command (continue.) • Once a variable has been made readonly in a shell, there is no way to undo it
The unset Command • Removes the definition of a variable from the environment or a function • Cannot unset: • readonly variables • IFS, MAIL, CHECK, PATH, PS1, PS2 $ v=100 $ echo $v 100 $ unset v $ echo $v removes v from the environment $
The eval Command • Format: eval command_line • eval makes the shell scan the command line twice before executing it $ pipe="|" $ ls $pipe wc -l |: No such file or directory wc: No such file or directory -l: No such file or directory $ eval ls $pipe wc -l 45 $
The wait Command • Format: wait process_id • If process_id is omitted, then the shell waits for all child process to complete execution $ sort data >sorted_data & 2517 $ ls addi function1.sql monitor3 procedure1.sql varfile6 args greetings mycp rem charis2 mail var memos creation1.sql personal varfile3 $ wait 2517 $
The $! Variable • Shell stores the process_id of the last command executed in the background in $! variable program1 & pid1=$: … program2 & pid2=$! … wait $pid1 * waits for program1 to finish … wait $pid2 * waits for program2 to finish
The trap Command • Format: trap command signals where commands is one or more commands that will be executed whenever any of the signals specified by signals is received • The commands that are specified to trap must be enclosed in quotes if they contain more than one command
The trap Command (continue.) • Shell scans the command line at: • the line when trap gets executed, and • when one of the listed signals is received • If commands are put inside double quotes, variables substitution will occur when trap is executed • If commands are put inside single quotes, variables substitution will occur when one of the signals is received $ trap "rm $HOME/foo$$; exit" 2 $ trap "rm $HOME/foo$$; exit" 21 $ trap `rm $HOME/foo$$; exit` 21
trap with no Arguments • trap with no arguments displays changed traps $ trap 'echo logged off at `date` >>$HOME/logoffs' 0 $ trap 0: echo logged off at `date` >>$HOME/logoffs $ egypt%
Redirect to standard error • Format: command >& file-descriptor redirects the standard output for command to file_descriptor • file_descriptor could be • 0 (standard input) • 1 (standard output) • 2 (standard error) • echo "Error.. ." >&2 writes an error to standard error
Redirect to standard error (continue.) • command >file 2>>file is identical to command >file 2>&1 where standard output s redirected to file and standard error is redirected to standard output
<&- and >&- • >&- closes standard output • <&- closes standard input • ls>&- will display nothing because standard output is closed
In_line Input Redirection –Here document (continue.) • ln_lineInput reads the input from the same file
In_line Input Redirection –Here document $ wc -l <<KEY > a > aa > aaa > aaaa > KEY 4 $
Functions • Format: name() { command; ... Coomand;} • Arguments listed after the function on the command line are assigned to the positional parameters $1, $2… • functions can not be passed to subshells • Changes made to the current directory or to variables remain after the function has completed execution • unset command is used to remove a definition of a function
Functions (continue.) $ cd UNIX $ db () { > PATH=$PATH:/$HOME/UNIX > PS1="`who am i`" > cd /; > } $ db sbenayed pts/2 Feb 1 08:13 (1Cust229.tnt28.nyc3.da.uu.net)
The return command • Format: return n • n: returns the status of the function • function returns the exit status of last command executed in the function if: • n is omitted • the function does not return command at the end • return status is equivalent to the exit status which its value can be accessed through the shell variable $?
The type command • type tells what is the comman: • function • shell program • shell buit_in command • standard UNIX command
The type command (continue.) $ mywho () { who am i; } $ type ls ls is /bin/ls $ type cat cat is /bin/cat $ type telnet telnet is /bin/telnet $ type mywho mywho is a function mywho(){ who am i } $
The restricted shellrsh • rsh restricts user from certain actions that the standard shell allows • rsh disallows: • change directory • change PATH or SHELL variables • specify a path to a command • redirect output (> and >>) • exec programs
The ulimit Command • Format: ulimit size • This command tells the shell to set the maximum size of files that can be written by child processes to sizeblocks $ ulimit unlimited $ ulimit 1000 $ ulimit 1000 $
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