特殊变量提醒:
$# 记录命令行参数个数
$* 保存所有参数,并当做单个单词保存
$@ 保存所有参数,当做同一个字符串中的多个独立的单词
getopts 命令格式:
getopts optstring variable
有效字母都会列在optstring中,当前参数保存在 variable中
示例:
#!/bin/bash while getopts :ab:c opt do case "$opt" in a) echo "Found the -a option";; b) echo "Found the -b option,wiht value $OPTARG";; c) echo "FOund the -c option";; *) echo "Unknown option :$opt";; esac done
测试
# sh test.sh -a -b test -c Found the -a option Found the -b option,wiht value test FOund the -c option
# sh test.sh -d Unknown option :?
选项字母要求有参数值的时候,在其后加一个冒号;
去掉错误消息的话,在optstring之前加一个冒号;
$OPTARG会保存参数值
出处:http://weipengfei.blog.51cto.com/1511707/1118695
以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索参数
, echo
, option
, 参数保存
, 冒号
, getopt
, found
The
,以便于您获取更多的相关知识。
时间: 2024-08-28 11:47:53