Two ways for the shell to pass parameters (detailed version)

The first

We can pass parameters to the script when executing the Shell script. The format of the parameters obtained in the script is: $n. n represents a number, 1 is the first parameter to execute the script, 2 is the second parameter to execute the script, and so on...

Example
In the following example, we pass three parameters to the script and output them respectively, where $0 is the file name to be executed:

#!/bin/bash
echo "Shell 传递参数实例!";
echo "执行的文件名:$0";
echo "第一个参数为:$1";
echo "第二个参数为:$2";
echo "第三个参数为:$3";

Set executable permissions for the script and execute the script. The output is as follows:

$ chmod +x test.sh 
$ ./test.sh 1 2 3

执行的文件名:./test.sh
第一个参数为:1
第二个参数为:2
第三个参数为:3

In addition, there are several special characters used to process parameters:

Parameter Handling Instructions

$#	传递到脚本的参数个数
$*	以一个单字符串显示所有向脚本传递的参数。
     如"$*"用「"」括起来的情况、以"$1 $2 … $n"的形式输出所有参数。
$$	脚本运行的当前进程ID号
$!	后台运行的最后一个进程的ID号
$@	与$*相同,但是使用时加引号,并在引号中返回每个参数。
    如"$@"用「"」括起来的情况、以"$1" "$2" … "$n" 的形式输出所有参数。
$-	显示Shell使用的当前选项,与set命令功能相同。
$?	显示最后命令的退出状态。0表示没有错误,其他任何值表明有错误。
#!/bin/bash
echo "Shell 传递参数实例!";
echo "第一个参数为:$1";
echo "参数个数为:$#";
echo "传递的参数作为一个字符串显示:$*";

Execute the script and the output is as follows:

$ chmod +x test.sh 
$ ./test.sh 1 2 3
第一个参数为:1
参数个数为:3
传递的参数作为一个字符串显示:1 2 3
 $* 与 $@ 区别:

The same point: all parameters are quoted.
Difference: Only reflected in double quotes. Assuming that three parameters 1, 2, 3 are written when the script is running, " * " is equivalent to "1 2 3" (one parameter is passed), and "@" is equivalent to "1" "2" " 3" (three arguments are passed).

#!/bin/bash
echo "-- \$* 演示 ---"
for i in "$*"; do
    echo $i
done

echo "-- \$@ 演示 ---"
for i in "$@"; do
    echo $i
done

Execute the script and the output is as follows:

$ chmod +x test.sh 
$ ./test.sh 1 2 3
-- $* 演示 ---
1 2 3
-- $@ 演示 ---
1
2
3

second getops

getopts is a tool for parsing script options arguments.

Syntax format:getopts [option[:]] [DESCPRITION] VARIABLE

option: represents an option that a script can use.

":": If a colon (":") appears after an option (option), it means that the option can be followed by a parameter (ie a piece of description information DESCPRITION)

VARIABLE : Indicates that an option is saved in the variable VARIABLE

getopts is a built-in variable in the linux system, which is generally used in loops. Whenever the execution loop is, getopts will check the next command options, if these options appear in option, it means it is a legal option, otherwise it is not a legal option. And save these legal options in the variable VARIABLE.
getopts also contains two built-in variables, OPTARG and OPTIND

  • OPTARG is to save the parameters (or description information DESCPRITION) after the option in this variable.
  • OPTIND: This represents the index of the next option or parameter on the command line (the filename does not count as an option or parameter)

Pay attention to the following points when using for the first time:

1) The script position parameter will match the single letter in optstring one by one. If it matches, it will be assigned to name, otherwise the assignment name will be a question mark;
2) A single letter in optstring is an option, if a colon is added after the letter, it means that the option is followed by a colon. Parameter and parameter value will be assigned to the OPTARG variable;
3) The first colon in optstring indicates a shielding system error (test.sh: illegal option — h);
4) It is allowed to put options together, such as -ab

experiment

1. The getops parameter is simple to use

#!/bin/bash
while getopts ':b:d:' OPT &> /dev/null;do
 case $OPT in
 b)
 echo "The options is b"
 echo $OPTARG ;;
 d)
 echo "The options is d"
 echo $OPTARG ;;
 *)
 echo "Wrong Options"
 exit 7 ;;
 esac
# echo $OPT
# echo $OPTARG
done
echo $OPTIND
shift $[$OPTIND-1]
echo $1

insert image description here
Explain the usage of getopts in shell script in detail.
Execution result:

 ./getopts1.sh -d 'nice' fixnale

insert image description here
illustrate:

When -d is entered, OPT = d , OPT=d,OPT=d , OPTARG='nice', so the information in this part of d)... will be displayed.

Since there is an option (-d) and an argument ('nice'), $OPTIND points to the index position of the next option or argument on the command line, so it is 3 here.

shift [ [ [ OPTIND-1] means to kick out options and arguments in front of the filename.

2. Further use of getops parameters

#!/bin/bash
echo $*
while getopts ":a:bc:" opt
do
 case $opt in
 a)
 echo $OPTARG $OPTIND;;
 b)
 echo "b $OPTIND";;
 c)
 echo "c $OPTIND";;
 ?)
 echo "error"
 exit 1;;
 esac
done
echo $OPTIND
shift $(( $OPTIND-1 ))
echo $0
echo $*

insert image description here
Explain the usage of getopts in shell script in detail.
Execution result:

./getopts2.sh -a 11 -b -c 6

Explain the use of getopts in shell scripts in detail
illustrate:

while getopts “:a:bc:” opt#The first colon means to ignore errors; a colon after the character means that the option must have its own parameter.

$optargStore the parameters of the corresponding options, such as 11 and 6 in the above example;

$optindAlways store the next option to be processed in the original $* (not a parameter, but an option, here refers to the three options a, b, c, not those numbers, of course, numbers will also occupy positions) position .

optindThe initial value is 1, when "x" is encountered, the option has no parameters, optind+=1; when "x:" is encountered, the option with parameters, optarg=argv[optind+1], optind+=2; when "x:" is encountered :", an optional parameter, one of #1 and #2.

第一行输出echo $*

第二行,optind初值为1,选项-a的参数为11,下一个要处理的选项-b位置为3,所以输出:11 3;

第三行,然后-b要处理的下一个选项-c位置为4,所以输出:b 4;

第四行,再者-c有参数,所以下一个要处理的位置+2,所以输出:c 6;

Guess you like

Origin blog.csdn.net/low5252/article/details/103646475