使用 getopts 解析带冒号的强制参数
Parsing obligatory parameter with colon using getopts
是否有可能在 bash 中使用 getopt 解析包含冒号的强制参数?
假设我准备了如下代码:
while getopts "qt:i:" arg; do
case "$arg" in
:)
HOST=$(printf "%s\n" ""| cut -d : -f 1)
PORT=$(printf "%s\n" ""| cut -d : -f 2)
shift 1
;;
-q)
QUIET=1
shift 1
;;
-t)
TIMEOUT=""
if [ "$TIMEOUT" = "" ]; then break; fi
shift 2
;;
-i)
INTERVAL=""
if [ "$INTERVAL" = "" ]; then break; fi
shift 2
;;
-h)
usage 0
;;
*)
echoerr "Unknown argument: "
usage 1
;;
esac
done
完整代码可在此处找到:https://pastebin.com/1eFsG8Qn
我如何调用脚本:
wait-for database:3306 -t 60 -i 10
问题是这个逻辑无法解析 HOST_URL:PORT。
任何提示如何解析它?
Is there possibility to parse obligatory argument containing colon using getopt in bash?
当然 - 他们必须在选项之后。
wait-for -t 60 -i 10 database:3306
然后:
while getopts .... ;do
case ...
blabla) .... ;;
esac
done
shift "$(($OPTIND-1))"
printf "Connect to %s\n" "" # here
参数中的冒号无论如何都不相关。
Any tips how to parse it?
使用 getopt
- 它重新排序参数。
是否有可能在 bash 中使用 getopt 解析包含冒号的强制参数?
假设我准备了如下代码:
while getopts "qt:i:" arg; do
case "$arg" in
:)
HOST=$(printf "%s\n" ""| cut -d : -f 1)
PORT=$(printf "%s\n" ""| cut -d : -f 2)
shift 1
;;
-q)
QUIET=1
shift 1
;;
-t)
TIMEOUT=""
if [ "$TIMEOUT" = "" ]; then break; fi
shift 2
;;
-i)
INTERVAL=""
if [ "$INTERVAL" = "" ]; then break; fi
shift 2
;;
-h)
usage 0
;;
*)
echoerr "Unknown argument: "
usage 1
;;
esac
done
完整代码可在此处找到:https://pastebin.com/1eFsG8Qn
我如何调用脚本:
wait-for database:3306 -t 60 -i 10
问题是这个逻辑无法解析 HOST_URL:PORT。 任何提示如何解析它?
Is there possibility to parse obligatory argument containing colon using getopt in bash?
当然 - 他们必须在选项之后。
wait-for -t 60 -i 10 database:3306
然后:
while getopts .... ;do
case ...
blabla) .... ;;
esac
done
shift "$(($OPTIND-1))"
printf "Connect to %s\n" "" # here
参数中的冒号无论如何都不相关。
Any tips how to parse it?
使用 getopt
- 它重新排序参数。