使用getopt命令.sh Shell 脚本

Shell 片段 · codes/shell/输入和输出/使用getopt命令.sh · 27 行

源码

#!/usr/bin/env bash

#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
	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

查看上游源码 ↗

内容与示例整理自 dunwu/linux-tutorial,遵循 CC BY-SA 4.0;本站保留原作者署名与许可,命令仅供学习,执行前请核对目标环境。