Pay attention to the shell script transfer parameters, the use of the shift command

Most applications can accept parameters in different formats. If -p, -v are optional, -k,N is another option that can accept numbers, and the command also requires a file name as a parameter. Then, it has the following execution methods:

  • $ command -p -v -k 1 file
  • $ command -pv -k 1 file
  • $ command -vpk 1 file
  • $ command file -pvk 1

The effects of the above methods are the same.
In the script, the command line parameters can be accessed according to their position in the command line. The first parameter is $1, the second parameter is $2, and
so on. So the first three command line parameters can be displayed like this
echo $1 $2 $3. The
more common processing method is actually to iterate all the command line parameters. shiftThe command can move the parameters one position to the left in turn, allowing the script to use $1 to access each parameter.
The following code shows all the command line parameters

[root@localhost ~]# cat showArgs.sh
for i in `seq 1 $#`
do
	echo $i is $1
	shift
done
[root@localhost ~]# ./showArgs.sh  a  b  c
1 is a
2 is b
3 is c
  • The seq command is a rounding command. Its usage is
    seq 1 5 means to list all integers between 1 and 5.
    Similarly, seq 1 $# means to list all integers between 1 and "number of all parameters"
  • Although the script did not write $2 and $3, the two parameters of bc are still read in, so after using shift, you can move the following parameters to the left one by one, so that you can access each parameter well.

Guess you like

Origin blog.csdn.net/qq_42428671/article/details/105885774