可能重复:
Using getopts in bash shell script to get long and short command line options
这是我编写的代码
#! /bin/bash
getopts master $1
while getopts ":master" opt; do
case $opt in
master)
echo "-master was triggered! $1 was entered" >&2
;;
\?)
echo "Invalid option: -$OPTARG" >&2
;;
esac
done
这是我得到的输出 -
]$ ./test123.sh -master 123
./test123.sh: line 3: getopts: `-master': not a valid identifier
如何定义用户定义的选项?
答案 0 :(得分:3)
我认为有几个问题。
getopts master $1
)正在做什么,你可以删除它e.g。以下似乎有效:
#!/bin/bash
while getopts ":m" opt; do
case $opt in
m)
echo "-m was triggered! $1 was entered" >&2
;;
\?)
echo "Invalid option: -$OPTARG" >&2
;;
esac
done