假设脚本名为test.sh
,选项例如-a -b -c
的形式。
#!/bin/bash
# extracting command line options as parameters
#
echo
while [ -n "$1" ]
do
case "$1" in
-a) echo "Found the -a option" ;;
-b) echo "Found the -b option" ;;
-c) echo "Found the -c option" ;;
*) echo "$1 is not an option" ;;
esac
shift
done
运行:
./test.sh -a -b -c -d
输出结果:
Found the -a option
Found the -b option
Found the -c option
-d is not an option
假如选项与参数例如-a -b -c -- test1 test2 test3
的形式。
#!/bin/bash
# extracting options and parameters
echo
# 识别选项
while [ -n "$1" ]
do
case "$1" in
-a) echo "Found the -a option" ;;
-b) echo "Found the -b option";;
-c) echo "Found the -c option" ;;
--) shift
break ;;
*) echo "$1 is not an option";;
esac
shift
done
# 分离参数
count=1
for param in $@
do
echo "Parameter #$count: $param"
count=$[ $count + 1 ]
done
运行:
./test.sh -a -b -c -- test1 test2 test3
输出结果:
Found the -c option
Found the -a option
Found the -b option
Parameter #1: test1
Parameter #2: test2
Parameter #3: test3
#!/bin/bash
# extracting command line options and values
echo
while [ -n "$1" ]
do
case "$1" in
-a) echo "Found the -a option";;
-b) param="$2"
echo "Found the -b option, with parameter value $param"
shift ;;
-c) echo "Found the -c option";;
--) shift
break ;;
*) echo "$1 is not an option";;
esac
shift
done
#
count=1
for param in "$@"
do
echo "Parameter #$count: $param"
count=$[ $count + 1 ]
done
运行:
./test.sh -a -b test1 -d
输出结果:
Found the -a option
Found the -b option, with parameter value test1
-d is not an option
getopt
命令可以接受一系列任意形式的命令行选项和参数,并自动将它们转换成适当的格式。它的命令格式如下:
getopt optstring parameters
其中optstring
定义了命令行有效的选项字母,还定义了哪些选项字母需要参数值。首先,在optstring
中列出你要在脚本中用到的每个命令行选项字母。然后,在每个需要参数值的选项字母后加一个冒号。getopt
命令会基于你定义的optstring
解析提供的parameters
。
getopt ab:cd -a -b test1 -cd test2 test3
# 输出
# -a -b test1 -c -d -- test2 test3
#!/bin/bash
# Extract command line options & values with getopt
# 使用getopt解析复杂选项,-q屏蔽错误选项
# set命令的选项之一是双破折线(--),它会将命令行参数替换成set命令的命令行值
set -- $(getopt -q ab:cd "$@")
echo
while [ -n "$1" ]
do
case "$1" in
-a) echo "Found the -a option" ;;
-b) param="$2"
echo "Found the -b option, with parameter value $param"
shift ;;
-c) echo "Found the -c option" ;;
--) shift
break ;;
*) echo "$1 is not an option";;
esac
shift
done
#
count=1
for param in "$@"
do
echo "Parameter #$count: $param"
count=$[ $count + 1 ]
done
./test.sh -ac
Found the -a option
Found the -c option
注意:getopt命令并不擅长处理带空格和引号的参数值。它会将空格当作参数分隔符,而不是根据双引号将二者当作一个参数。
./test.sh -a -b test1 -cd "test2 test3" test4
Found the -a option
Found the -b option, with parameter value 'test1'
Found the -c option
Parameter #1: 'test2
Parameter #2: test3'
Parameter #3: 'test4'
getopts
命令与getopt
不同,前者将命令行上选项和参数处理后只生成一个输出,而getopts
命令能够和已有的shell参数变量配合默契。
getopts optstring variable
optstring
值类似于getopt
命令中的那个。有效的选项字母都会列在optstring
中,可以在参数值中包含空格。如果选项字母要求有个参数值,就加一个冒号。要去掉错误消息的话,可以在optstring
之前加一个冒号。getopts
命令将当前参数保存在命令行中定义的variable
中。
#!/bin/bash
# simple demonstration of the getopts command
#
echo
while getopts :ab:c opt
do
case "$opt" in
a) echo "Found the -a option" ;;
b) echo "Found the -b option, with value $OPTARG";;
c) echo "Found the -c option" ;;
*) echo "Unknown option: $opt";;
esac
done
运行:
./test.sh -ab test1 -c
结果输出:
Found the -a option
Found the -b option, with value test1
Found the -c option
另一个好用的功能是将选项字母和参数值放在一起使用,而不用加空格。
./test.sh -abtest1
Found the -a option
Found the -b option, with value test1
完整例子
#!/bin/bash
# Processing options & parameters with getopts
#
echo
while getopts :ab:cd opt
do
case "$opt" in
a) echo "Found the -a option" ;;
b) echo "Found the -b option, with value $OPTARG" ;;
c) echo "Found the -c option" ;;
d) echo "Found the -d option" ;;
*) echo "Unknown option: $opt" ;;
esac
done
#
shift $[ $OPTIND - 1 ]
#
echo
count=1
for param in "$@"
do
echo "Parameter $count: $param"
count=$[ $count + 1 ]
done
OPTIND
环境变量保存了参数列表中getopts
正在处理的参数位置。这样你就能在处理完选项之后继续处理其他命令行参数了
常用的Linux命令选项规范
选 项 | 描 述 |
---|---|
-a | 显示所有对象 |
-c | 生成一个计数 |
-d | 指定一个目录 |
-e | 扩展一个对象 |
-f | 指定读入数据的文件 |
-h | 显示命令的帮助信息 |
-i | 忽略文本大小写 |
-l | 产生输出的长格式版本 |
-n | 使用非交互模式(批处理) |
-o | 将所有输出重定向到的指定的输出文件 |
-q | 以安静模式运行 |
-r | 递归地处理目录和文件 |
-s | 以安静模式运行 |
-v | 生成详细输出 |
-x | 排除某个对象 |
-y | 对所有问题回答yes |