Académique Documents
Professionnel Documents
Culture Documents
html
Positional Parameters:
Arguments are passed from the command line into a shell program using the
positional parameters $1 through to $9. Each parameter corresponds to the
position of the argument on the command line.
The first argument is read by the shell into the parameter $1, The second
argument into $2, and so on. After $9, the arguments must be enclosed in
brackets, for example, ${10}, ${11}, ${12}.Some shells doesn't support
this
method. In that case, to refer to parameters with numbers greater than 9,
use
the shift command; this shifts the parameter list to the left. $1 is
lost,while
$2 becomes $1, $3 becomes $2, and so on. The inaccessible tenth parameter
becomes $9 and can then be referred to.
Example:
#!/bin/bash
# Call this script with at least 3 parameters, for example
# sh scriptname 1 2 3
echo "first parameter is $1"
echo "Second parameter is $2"
echo "Third parameter is $3"
exit 0
Output:
[root@localhost ~]# sh parameters.sh 47 9 34
first parameter is 47
Second parameter is 9
Third parameter is 34
[root@localhost ~]# sh parameters.sh 4 8 3
first parameter is 4
Second parameter is 8
Third parameter is 3
In addition to these positional parameters, there are a few other special
parameters used by shell.Their significance is noted bellow.
$* - It stores the complete set of positional parameters as a single
string.
$@ - Same as $*, But there is subtle difference when enclosed in double
quotes.
$# - It is set to the number of arguments specified.This lets you design
scripts
that check whether the right number of arguments have been entered.
$0 - Refers to the name of the script itself.
$ echo $*
Helping hands are holier than praying lips
$ echo $@
Helping hands are holier than praying lips
$ d=$4
$ shift 4
$ echo $a $b $c $d $1 $2 $3 $4 $5 $6 $7 $8 $9
A friend who helps when one is in trouble is a real friend
Note:In the Korn and bash shells you can refer directly to arguments
where
n is greater than 9 using braces. For example, to refer to the 57th
positional
parameter, use the notation ${57}.some shells may not support this method.
$ set A friend who helps when one is in trouble is a real friend
$ echo ${12}
real
$ echo ${13}
friend
Bracket notation for positional parameters leads to a fairly simple way
of
referencing the last argument passed to a script on the command line.
$ echo ${!#}
friend
$* and $@
Let us look at the subtle difference between $* and $@.
First create three files
cat > fileA
I LOVE INDIA
Ctrl+d
Example:
#!/bin/bash
# Usage: sh arguments.sh fileA fileB temp\ file
echo -e "\033[1mDisplay files content using \$* \033[0m"
cat $*
echo -e "\033[1mDisplay files content using \$@ \033[0m"
cat $@
exit 0
Run the script with file names as arguments.
Output:
[root@localhost ~]# sh arguments.sh fileA fileB temp\ file
Display files content using $*
I LOVE INDIA
HELLO WORLD
cat: temp: No such file or directory
cat: file: No such file or directory
Display files content using $@
I LOVE INDIA
HELLO WORLD
cat: temp: No such file or directory
cat: file: No such file or directory
So there is no difference between cat $* and cat $@.
Modify arguments.sh script as
#!/bin/bash
# Usage: sh arguments.sh fileA fileB temp\ file
echo -e "\033[1mDisplay files content using \"\$*\" \033[0m"
cat "$*"
echo -e "\033[1mDisplay files content
cat "$@"
exit 0
Now again run the script with file names as arguments.
Output:
[root@localhost ~]# sh arguments.sh fileA fileB temp\ file
Display files content using "$*"
cat: fileA fileB temp file: No such file or directory
http://faculty.salina.k-state.edu/tim/unix_sg/bash/math.html
As we will see, some of the commands used to facilitate math are a little picky about things like
spaces around operators.
declare
You may recall, that when the text book introduced the declare statement, it said that it is not
always needed. So what do you get by declaring a variable to be an integer? The following
example illustrates that a declared integer is not treated as a string.
$ n=6/3
$ echo $n
6/3
$ declare -i n
$ n=6/3
$ echo $n
2
When you do not need the declare statement is when you will use a program or built-in
command to evaluate a math statement.
expr
An old Unix program that can evaluate math is expr. expr became popular in the days of the
Bourne Shell, which did not support math. With Bash and Korn shell, it is generally not needed.
Since it is a command, command substitution is needed. We are still treating the variable as a
string. As you can see, it is picky about spaces.
$ z=5
$ z=`expr $z+1`
---- Need spaces around + sign.
$ echo $z
5+1
$ z=`expr $z + 1`
$ echo $z
6
let
A Bash and Korn shell built-in command for math is let. As you can see, it is also a little picky
about spaces, but it wants the opposite of what expr wanted. let also relaxes the normal rule of
needing a $ in front of variables to be read.
$ let z=5
$ echo $z
5
$ let z=$z+1
$ echo $z
6
$ let z=$z + 1
# --- Spaces around + sign are bad with let
-bash: let: +: syntax error: operand expected (error token is "+")
$let z=z+1
$echo $z
7
An alternate form of let is to wrap the whole statement in double parenthesis. This form is more
forgiving about spaces.
$ ((e=5))
$ echo $e
5
$ (( e = e + 3 ))
$ echo $e
8
$ (( e=e+4 )) # -- spaces or no spaces, it doesn't matter
$ echo $e
12
bc
What if you want to do math with floating point numbers or you have some fairly complicated
math to do? Neither form of let, supports floating point numbers. The bc command is needed.
But you have to treat the variables as strings.
Here is what happens when we try to do floating point math with the shell:
$let r=3.5
-bash: let: r=3.5: syntax error in expression (error token is ".5")
$(( r = 3.5 ))
-bash: ((: r = 3.5 : syntax error in expression (error token is ".5 ")
bc
Note that the arithmetic tests shown in Figure 6-27, page 223, must be used with the (( )) form of
let. Otherwise, according to the test man page, the following numeric tests are used.
INTEGER1 -eq INTEGER2
INTEGER1 is equal to INTEGER2
INTEGER1 -ge INTEGER2
INTEGER1 is greater than or equal to INTEGER2
INTEGER1 -gt INTEGER2
INTEGER1 is greater than INTEGER2
INTEGER1 -le INTEGER2
INTEGER1 is less than or equal to INTEGER2
INTEGER1 -lt INTEGER2
INTEGER1 is less than INTEGER2
INTEGER1 -ne INTEGER2
INTEGER1 is not equal to INTEGER2
if [ $x -lt $y ]; then
statements
fi
And for logical expressions using floating point math, the bc command returns 1 for logical true
expressions and 0 for false expressions, thus testing for result of 1 achieves a logical expression:
if [ $( echo "$t < 3.4" | bc ) -eq 1 ]; then
statements
fi