linux-tutorial/codes/shell/示例脚本/输入和输出/使用getopt命令.sh

27 lines
591 B
Bash
Raw Normal View History

2019-05-10 11:17:57 +08:00
#!/bin/bash
2019-10-10 08:56:31 +08:00
2019-05-10 11:17:57 +08:00
#extracting command line options and values with getopt
# getopt command is not goot at dealing with space,we can use getopts
set -- `getopt -q ab:c "$@"`
while [ -n "$1" ]
do
2019-10-10 08:56:31 +08:00
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
2019-05-10 11:17:57 +08:00
done
count=1
for param in "$@"
do
2019-10-10 08:56:31 +08:00
echo "Parameter #$count: $param"
count=$[ $count + 1 ]
2019-05-10 11:17:57 +08:00
done