Ski*_*les 2 shell getopt getopts
有人可以检查此代码片段并告诉我为什么当我使用 -p abcdef 调用此脚本时 $OPTARG 从未具有传入的参数值?
# Process command-line options passed as switches to this script
while getopts "ph:" option; do
case "$option" in
p)
{
if [ -n "$OPTARG" ]; then
echo
echo "##### SCRIPT ERROR: You failed to provide a host prefix. #####"
echo
usage
break
else
echo "Setting host prefix to '$OPTARG'"
echo
HOST_PREFIX=$OPTARG
fi
} ;;
h) usage ;;
'?') usage ;;
*) break ;;
esac
done
shift "$((OPTIND-1))" # Shift off the options and optional --.
Run Code Online (Sandbox Code Playgroud)