The usage of Shift in Shell programming

Positional parameters can be shifted left using the shift command. For example, shift 3 means that the original $4 is now $1, the original $5 is now $2, etc. The original $1, $2, and $3 are discarded, and $0 does not move. The shift command without arguments is equivalent to shift 1.
Very useful Unix command: shift. We know that for positional variables or command line parameters, the number must be determined, or when the Shell program does not know the number, all parameters can be assigned to the variable $* together. If the user asks the Shell to process the parameters one by one without knowing the number of position variables, that is, $2 after $1, $3 after $2, and so on. The value of the variable $1 before the shift command is executed is not available after the shift command is executed.

The example is as follows:

#Test shift command(x_shift.sh)
until [ $# -eq 0 ]
do
echo "The first parameter is: $1 The number of parameters is: $#"
shift
doneExecute
the above program x_shift.sh:
$./ x_shift.sh 1 2 3 4

The result is shown as follows:

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

It can be seen from the above that each time the shift command is executed, the number of variables ($#) is decremented by one, and the variable value is advanced by one. The following code uses the until and shift commands to calculate all commands The sum of the row parameters.

#shift application of shift command (x_shift2.sh)
if [ $# -eq 0 ]
then
echo "Usage:x_shift2.sh parameter"
exit 1
fi
sum=0
until [ $# -eq 0 ]
do
sum=`expr $ sum + $1`
shift
done
echo "sum is: $sum"

Execute the above program:

$x_shift2.sh 10 20 15

The display result is:

45

  Shift command has another important purpose, Bsh defines 9 position variables, starting from $1 To $9, this does not mean that the user can only use 9 parameters on the command line, with the help of the shift command can access more than 9 parameters.

  The number of parameters to be moved by the Shift command at a time is specified by the parameters it carries. For example, after the shell program has processed the first nine command-line arguments, you can use the shift 9 command to move $10 to $1.

https://www.cnblogs.com/image-eye/archive/2011/08/20/2147153.html


Linux shell variables $0, $? and other meanings
Linux shell variables $#, $@, $0, $1, $2 The basic meaning of:
variable description:
$$
The PID of the Shell itself (ProcessID)
$!
The PID of the background Process that the Shell runs last
$?
The end code (return value) of the last running command
$
-List of Flags set by the Set command
$* List
of all parameters. For example, when "$*" is enclosed in """, all parameters are output in the form of "$1 $2 ... $n".
$@
A list of all parameters. For example, when "$@" is enclosed in """, all parameters are output in the form of "$1" "$2" … "$n".
$#
The number of parameters added to the shell
$0
The file name of the shell itself
$1~$n
are added to each parameter value of the shell. $1 is the first parameter, $2 is the second parameter ...

http://www.cnblogs.com/coffy/p/5740396.html

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326085389&siteId=291194637