0% found this document useful (0 votes)
79 views18 pages

Linux Bourne Again Shell (Bash) Programming: Vsrivera Ibm Learning Services Worldwide Certified Manual

This document discusses positional parameters, variable parameters, and return values in Bash shell scripts. Positional parameters allow passing arguments to scripts, referenced as $1, $2, etc. Variable parameters include special variables like $#, $@, $*, and others. Commands return exit status codes stored in $?, which can be used for conditional execution. Numeric and string expressions like -eq and = allow testing parameters and values.

Uploaded by

vsrivera
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
79 views18 pages

Linux Bourne Again Shell (Bash) Programming: Vsrivera Ibm Learning Services Worldwide Certified Manual

This document discusses positional parameters, variable parameters, and return values in Bash shell scripts. Positional parameters allow passing arguments to scripts, referenced as $1, $2, etc. Variable parameters include special variables like $#, $@, $*, and others. Commands return exit status codes stored in $?, which can be used for conditional execution. Numeric and string expressions like -eq and = allow testing parameters and values.

Uploaded by

vsrivera
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 18

Linux Bourne Again Shell

(BASh) Programming

VSRivera
IBM Learning Services
Worldwide Certified Manual
Positional Parameters
 Parameters can be passed to Shell
Scripts as arguments on the
command line
 Param.sh arg1 arg2
 “Arg1” is the positional parameter # 1
 “Arg2” is the positional parameter # 1
 Others are unset
Positional Parameters
 Positional Parameters are referenced
in the script by:
 $0 is the name of the script itself
 $1 to $9 for the first nine
 ${10} to ${n} for the remainder (Bash
only)
 If you want to pass arguments that
begin with “-” or “++, you can use the
convention that “- -” marks the end of
options for command or script.
Positional Parameters
 Param.sh -- -arg1 +arg2 arg3
 This will prevent “-arg1” being treated as
an option rather than an argument
 The special variables $* and $@ denote
all the positional parameters.
Setting Positional Parameter
 The set command can
 Change the values of Positional
Parameters
 Unset the Positional Parameters
previously set
cat first.sh
echo $1 $2 $3
set value1 value2 value3
echo $1 $2 $3
Setting Positional Parameter
first.sh a b c
abc
value1 value2 value3
___________
 Set cleared the 3 parameters
 The unset command can also be used to
clear a variable from memory and so
remove it
unset var1 or unset –v var1
Variable Parameters
 Shell scripts set a number of other
Shell Parameters
 S# - the number of positional
parameters set
 $@ - Positional Parameters in a space
separated list
 $* - Positional Parameters in a list
separated by the first Field Separator
(the default is a space)
Variable Parameters
 In double quotes, $@ and $* behave
differently
 “$@” = “$1” “$2” “$3” …
 “$*” = “$1 $2 $3 …”
Shell Parameters
 Shell Parameters that remain fixed
for the duration of the script:
 $0 the (path)name used to invoke the
shell script
 $$ the process ID (PID) of current
process (shell)
 $- Shell options used to invoke the shell
 S! the PID of the last backgroud process
 $? The return code from the last
command executed
Shift Command
 The shift command reassigns the positional
parameters, in effect shifting them to the
left one notch.
 $1 <--- $2, $2 <--- $3, $3 <--- $4, etc.
 The old $1 disappears, but $0 (the script
name) does not change. If you use a large
number of positional parameters to a
script, shift lets you access those past 10,
although {bracket} notation also permits
this.
 The shift command can take a
numerical parameter indicating how
many positions to shift.
#!/bin/bash
# shift-past.sh
shift 3 # Shift 3 positions.
# n=3; shift $n
# Has the same effect.
echo "$1"
Return Values
 Each command, pipeline or group of
commands returns a value to its parent
process
 $? Contains the value of the return code
 zero mean success
 non-zero means an error occurred
 The single value returned by a pipeline is
the return code of the last command in the
pipeline.
Exit Status
 $ echo $$ check the shell process ID
879
 $ bash start a new sub-shell
 $ echo $$
880
 $ exit quit the sub-shell
 $ echo $? And print the return code
0
 $ echo $$
879
Exit Status
 $ bash begin another sub-shell
 $ echo $$
890
 $ exit 101 exit with a value to set
 $ echo $? The return code
101
 $ echo $$
879
 The exit command is a shell builtin
command
Conditional Execution
 A return code (or exit status) can be used
to determine whether or not to execute the
next command
 Id command1 is successful execute command2
 Command1 && command2
 $ rm –f file1 && echo file1 removed
 If command1 is not successfule execute
command 2
 Command1 || command2
 who | grep Kate || echo Kate logged off
Numeric Expressions
 For the arithmetic expressions and
integer values use
 exp1 –eq exp2
 exp1 –ne exp2
 exp1 –lt exp2
 exp1 –le exp2
 exp1 –gt exp2
 exp1 –ge exp2
Numerical Expressions
 $ x=2
 $ test $x –eq 1 or
 $ [ $x – eq 1 ]
String Expressions
 To examine strings use one of the
following:
 -n str
 -z str
 str1 = str2
 str1 != str2
 $ h=hello
 $ test $h = hello

You might also like