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

42

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

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

Parameter substitution

Parameter Meaning

$parameter or${parameter}

Substitute the value of parameter

${parameter:-value}

Substitute the value of parameter if it's non-null; otherwise, substitute value

${parameter:=value}

Substitute the value of parameter if it's non-null; otherwise substitute value and also assign it to parameter

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

Parameter substitution (continue.)

Parameter Meaning

${parameter:?value}

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:+value}

Substitute value if parameter is non-null; otherwise, substitute nothing

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

The $0 variable

Shell stores the name of the program inside the special variable $0$ cat foo

$0 is running!!$ foofoo: cannot fork: no swap space$

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

The set Command

Option Meaning

-- Don't treat subsequent args preceded by a – as options

-a Automatically export all variables that are subsequently defined or modified

-e Exit if any command that gets executed has a nonzero exit status

-f Disable file name generation

-f Remember the locations of commands used in functions when the functions are defined (see hash command)

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

The set Command (continue.)

Option Meaning

-k Process 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

-n Read commands without executing them (useful for checking for balanced do…dones, and if…fis)

-t Exit after executing one command

-u Issue and error if a variable is referenced without having been assigned a value or if a positional parameter is referenced without having been set

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

The set Command (continue.)

Option Meaning

-v Print each shell command line as it is read

-x Print each command and its arguments as it is executed, preceded by a +

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

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

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

The –x option (continue.)

$ x=*$ set -x set command trace option$ echo $*+ echo set command trace optionset command trace option$ mysl=lsmysl=ls $ ls | wc -l+ ls+ wc -l 32$

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

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/sbenayedSHELL=/bin/cshTCAT=/usr/lib/lp/postscript/dpostTERM=vt100TZ=US/EasternUNIX=/home/abuzneid/UNIXUSER=sbenayedmysl=lsx=*

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

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:da:b:c:d$ echo $#+ echo 44$ echo $*+ echo a b c da b c d$ for arg; do echo $arg; donesyntax error: `;' unexpected$

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

Using set to reassign Positional Parameters (continue.)

## Count words on a line#read lineset $lineecho $#$ parse+ parseI love New York city6$

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

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

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

Shell Program: parse Counts all the words on standard input To view the source code of parse click here

$ parse < $HOME/.profile2$ wc -w < $HOME/.profile 12$

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

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 -b0000000 040 011 012 0120000004

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

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$

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

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; donesyntax error: `;' unexpected

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

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$

$ readonlyreadonly MYPATH

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

The readonly Command (continue.)

Once a variable has been made readonly in a shell, there is no way to undo it

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

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 $v100$ unset v$ echo $v removes v from the environment

$

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

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 directorywc: No such file or directory-l: No such file or directory$ eval ls $pipe wc -l 45$

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

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$ lsaddi function1.sql monitor3 procedure1.sql varfile6 args greetings mycp rem charis2 mail var memos creation1.sql personal varfile3$ wait 2517$

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

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

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

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

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

The trap Command (continue.)

Signal Generated for

0 Exit for the shell

1 Hangup

2 Interrupt (e.g., Delete key)

3 Quit

4 Illegal instruction

5 Trace trap

6 IOT instruction

7 EMT instruction

8 Floating point exception

10 Bus error

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

The trap Command (continue.)

Signal Generated for

12 Bad argument to a system call

13 Write to a pipe without a process to read it

14 Alarm timeout

15 Software termination signal (sent by kill by default)

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

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

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

trap with no Arguments

trap with no arguments displays changed traps

$ trap 'echo logged off at `date` >>$HOME/logoffs' 0$ trap0: echo logged off at `date` >>$HOME/logoffs$ egypt%

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

Ignoring signals

trap " " 2 trap 2 trap : 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

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

Redirect to standard error

Format: command >& file-descriptorredirects the standard output for command to file_descriptor

file_descriptor could be 0 (standard input) 1 (standard output) 2 (standard error)

echo "Error.. ." >&2writes an error to standard error

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

Redirect to standard error (continue.)

command >file 2>>fileis identical to

command >file 2>&1where standard output s redirected to file and

standard error is redirected to standard output

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

<&- and >&-

>&- closes standard output <&- closes standard input ls >&-

will display nothing because standard output is closed

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

In_line Input Redirection –Here document (continue.)

ln_line Input reads the input from the same file

command<<KEY command<<\KEY command<<-KEY

All the lines beween the command and the second key is used as standard input

Same 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

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

In_line Input Redirection –Here document

$ wc -l <<KEY> a> aa> aaa> aaaa> KEY 4$

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

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

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

Functions (continue.)

$ cd UNIX$ db () {> PATH=$PATH:/$HOME/UNIX> PS1="`who am i`"> cd /;> }$ dbsbenayed pts/2 Feb 1 08:13 (1Cust229.tnt28.nyc3.da.uu.net)

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

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

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

The type command

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

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

The type command (continue.)

$ mywho () { who am i; }$ type lsls is /bin/ls$ type catcat is /bin/cat$ type telnettelnet is /bin/telnet$ type mywhomywho is a functionmywho(){who am i}$

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

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

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

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

$ ulimitunlimited$ ulimit 1000$ ulimit1000$

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

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