Presentation is loading. Please wait.

Presentation is loading. Please wait.

Parameter substitution ParameterMeaning $parameter or ${parameter} Substitute the value of parameter ${parameter:- value} Substitute the value of parameter.

Similar presentations


Presentation on theme: "Parameter substitution ParameterMeaning $parameter or ${parameter} Substitute the value of parameter ${parameter:- value} Substitute the value of parameter."— Presentation transcript:

1

2 Parameter substitution ParameterMeaning $parameter or ${parameter} Substitute the value of parameter ${parameter:- value} Substitute the value of parameter if it's non-null; otherwise, substitute value ${parameter:=va lue} Substitute the value of parameter if it's non-null; otherwise substitute value and also assign it to parameter

3 Parameter substitution (continue.) ParameterMeaning ${parameter:?v alue} Substitute the value of parameter if it's non-null; otherwise, write value to standard error and exit. If value is omitted, then write parameter: Parameter null or not set instead ${parameter:+v alue} Substitute value if parameter is non-null; otherwise, substitute nothing

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

5 The set Command OptionMeaning --Don't treat subsequent args preceded by a – as options -aAutomatically export all variables that are subsequently defined or modified -eExit if any command that gets executed has a nonzero exit status -fDisable file name generation -fRemember the locations of commands used in functions when the functions are defined (see hash command)

6 The set Command (continue.) OptionMeaning -kProcess arguments of the form keyword=value that appear anywhere on the command line (not just before the command) and place them in the environment of the command -nRead commands without executing them (useful for checking for balanced do…dones, and if…fis) -tExit after executing one command -uIssue and error if a variable is referenced without having been assigned a value or if a positional parameter is referenced without having been set

7 The set Command (continue.) OptionMeaning -vPrint each shell command line as it is read -xPrint each command and its arguments as it is executed, preceded by a +

8 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

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

10 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=*

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

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

13 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

14 Shell Program: parse Counts all the words on standard input To view the source code of parse click herehere $ parse < $HOME/.profile 2 $ wc -w < $HOME/.profile 12 $

15 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

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

17 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

18 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

19 The readonly Command (continue.) Once a variable has been made readonly in a shell, there is no way to undo it

20 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 $vremoves v from the environment $

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

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

23 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

24 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

25 The trap Command (continue.) SignalGenerated for 0Exit for the shell 1Hangup 2Interrupt (e.g., Delete key) 3Quit 4Illegal instruction 5Trace trap 6IOT instruction 7EMT instruction 8Floating point exception 10Bus error

26 The trap Command (continue.) SignalGenerated for 12Bad argument to a system call 13Write to a pipe without a process to read it 14Alarm timeout 15Software termination signal (sent by kill by default)

27 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

28 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%

29 Ignoring signals trap " " 2trap 2trap : 2 Current shell and all subshells ignore signal 2 Resets to the default action Current shell will ignore signal 2 but all active subshells to take the default action will receive signal 2

30 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

31 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

32 &- >&- closes standard output <&- closes standard input ls >&- will display nothing because standard output is closed

33 In_line Input Redirection –Here document (continue.) ln_line Input reads the input from the same file command<<KEYcommand<<\KEYcommand<<-KEY All the lines beween the command and the second key is used as standard input Same Shell: - performs parameter substitution - Executes back_quoted commands - Recognizes the backslash chracter shell leaves the input lines completely untouched Shel removes leading tab character in the input

34 In_line Input Redirection –Here document $ wc -l <<KEY > a > aa > aaa > aaaa > KEY 4 $

35 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

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

37 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 $?

38 The type command type tells what is the comman: function shell program shell buit_in command standard UNIX command

39 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 } $

40 The restricted shell rsh 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

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

42 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 "Parameter substitution ParameterMeaning $parameter or ${parameter} Substitute the value of parameter ${parameter:- value} Substitute the value of parameter."

Similar presentations


Ads by Google