Shell脚本编程

$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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

robin-cloud

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值