SH – shell parametric

Time:2019-8-13

Shell data transmission

Reference related in shell:

./a.sh a b c d

& dollar; #: The number of parameters passed to the script

& dollar; 0: The name of the script itself

& dollar; 1: is the first parameter passed to the shell script

& dollar; 2: is the second parameter passed to the shell script

& dollar;@: A list of all the parameters passed to the script

& dollar;*: Displays all parameters passed to the script in a single string, which, unlike location variables, can exceed9individual

& dollar; & dollar;: is the current process ID number of the script running

& dollar;?: Displays the exit status of the final command.0Represents no errors, other representations have errors

There is no difference between and @. If the quotation marks () and (@) are different, “” takes the parameters as a string, and “@” divides them into parameter arrays according to the delimiter.

#!/bin/sh
num=$#
name=$0
echo "para num: $num"
echo "shell name: $name"
 
Echo
for key in [email protected]
do
    echo $key
done
 
Echo "--------- traversal"$"--------------"
for key in "[email protected]"
do
    echo $key
done
 
Echo "------------- traversing $*--------"
for key2 in $*
do
    echo $key2
done
Echo "----- traversal"$*"-----------"
for key2 in "$*"
do
    echo $key2
done
 
echo "show process id:$$"
echo "show precomm stat: $?"
para num: 3
shell name: ./test1.sh
Traverse a 12 - ----------------------------------------------------------------------------------------------------------------------
a
1
2
Traverse a 12----------------------------------------------------------------------------------------------------------------------
a
1
2
Traverse a 12----------------------------------------------------------------------------------------------------------------------
a
1
2
Traverse a 12------------------------------------------------------------------------------------------------------.
a 1 2
show process id:10913
show precomm stat: 0

Using shift traversal parameters

Position parameters can be usedshiftThe command moves left. such asshift 3Represents the original$4Now it becomes$1The original$5Now it becomes$2Wait a minute, original$1$2$3Discard,$0No movement. ParametricshiftThe command corresponds toshift 1

Very useful Unix command: shift. We know that the number of position variables or command line parameters must be determined, or when the shell program does not know the number, all parameters can be assigned to the variable. If the user asks Shell to process the parameters one by one without knowing the number of position variables, that is, in. If the user asks Shell to process the parameters one by one without knowing the number of position variables, that is, 2 after 1, 2 after 2 and 3 after 2. Variable 3, etc. before the shift command is executed. The value of variable 1 before the shift command is executed is not available after the shift command is executed.

Examples are as follows:

Test the shift command (x_shift.sh)

#!/bin/sh
until [ $# -eq 0 ]
do
Echo: "The first parameter is: $1 parameter number: $"
shift
done

Execute the above program x_shift.sh:

./xshift.sh 1234

The first parameter is: 1. The number of parameters is: 4.
The first parameter is: 2 parameter number is: 3
The first parameter is: 3 parameters: 2
The first parameter is: 4 parameters: 1

It can be seen from the above that every time the shift command is executed, the number of variables (#) is reduced by one, and the value of variables is one bit ahead of time. The following code calculates the sum of all command line parameters with until and shift commands.

# Application of shift Up-File command (x_shift 2.sh)

#!/bin/sh
if [ $# -eq 0 ]
then
Echo "Usage: x_shift 2.sh parameter"
exit 1
fi
sum=0
until [ $# -eq 0 ]
do
sum=`expr $sum + $1`
shift
done
echo "sum is: $sum"

Implement the above procedures:

x_shift2.sh 10 20 15

Result45

The Shift command has another important use. Bsh defines nine location variables, ranging from 1 to 9. This does not mean that users can only use nine parameters on the command line, and can access more than nine parameters with the shift command.

The Shift command moves the number of parameters at a time specified by the parameters it carries. For example, when the shell program has processed the first nine command line parameters, it can use the shift 9 command to move 10 to $1.

If you want to know the parameter subscript when traversing the parameter again, you can use shift.

#!/bin/sh
iter=1
until [ $# -eq 0 ]
do
    echo $1, $iter
    ((iter=iter+1)) #let iter=$iter+1
    shift
done