$0 # 脚本本身
$1 # 脚本第一个参数
$2 # 脚本第二个参数
$* # 所有参数列表(字符串形式:如 test.sh 1 2 3, 则$* 为“1 2 3”)
$@ # 所有参数列表(字符串数组,如 test.sh 1 2 3, 则$@为 “1” “2” “3”)
$$ # 脚本运行的PID
$? # 脚本退出码
$() #等同于:
$(()) #进行数字运算
如,
# a=1;b=3;c=6;
# echo $((a+b*c))
getopt 命令
getopt 命令可以接受一系列任意形式的命令行选项和参数,并自动将他们转成适当的格式。
# getopt -help
Usage:
getopt optstring parameters
getopt [options] [--] optstring parameters
getopt [options] -o|--options optstring [options] [--] parameters
Options:
-a, --alternative Allow long options starting with single -
-h, --help This small usage guide
-l, --longoptions <longopts> Long options to be recognized
-n, --name <progname> The name under which errors are reported
-o, --options <optstring> Short options to be recognized
-q, --quiet Disable error reporting by getopt(3)
-Q, --quiet-output No normal output
-s, --shell <shell> Set shell quoting conventions
-T, --test Test for getopt(1) version
-u, --unquoted Do not quote the output
-V, --version Output version information
getopt的命令格式如下:
getopt optstring parameters
optstring是关键所在,它定义命令有效的选项字母,还定义了哪些选项字母需要参数值。
列如:
# getopt ab:cd -a -b test1 -cd test2 test3
-a -b test1 -c -d -- test2 test3
optstring定义了四个有效选项字母:a,b,c,d.冒号(:)被放在了字母b后面,因为b选项需要一个参数值,当getopt命令运行时,它会检查提供的参数列表(-a -b test1 -cd test2 test3),并给予提供的optstring进行解析,它会自动讲-cd选项分成两个单独的选项,并插入双破折线来分割行中的额外参数。
https://cloud.tencent.com/developer/article/1