【发布时间】:2018-06-15 16:40:18
【问题描述】:
我在我的 bash_aliases 中创建了一个脚本,以便更轻松地通过 SSH 连接到服务器。但是,我遇到了一些我不理解的奇怪行为。以下脚本按您的预期工作,除非它被重复使用。
如果我第一次在 shell 中这样使用它,它会完全按预期工作:
$>sdev -s myservername
ssh -i ~/.ssh/id_rsa currentuser@myservername.devdomain.com
但是,如果我第二次运行它,而不指定 -s|--server,它将使用我上次运行时的服务器名称,似乎已经缓存了它:
$>sdev
ssh -i ~/.ssh/id_rsa currentuser@myservername.devdomain.com
它应该退出并出现错误并输出此消息:/bin/bash: A server name (-s|--server) is required.
这发生在任何参数上;也就是说,如果我指定了一个参数,然后下次我不指定,此方法将使用上次提供的参数。
显然,这不是我想要的行为。我的脚本中有什么责任这样做,我该如何解决?
#!/bin/bash
sdev() {
getopt --test > /dev/null
if [[ $? -ne 4 ]]; then
echo "`getopt --test` failed in this environment"
exit 1
fi
OPTIONS=u:,k:,p,s:
LONGOPTIONS=user:,key:,prod,server:
# -temporarily store output to be able to check for errors
# -e.g. use “--options” parameter by name to activate quoting/enhanced mode
# -pass arguments only via -- "$@" to separate them correctly
PARSED=$(getopt --options=$OPTIONS --longoptions=$LONGOPTIONS --name "$0" -- "$@")
if [[ $? -ne 0 ]]; then
# e.g. $? == 1
# then getopt has complained about wrong arguments to stdout
exit 2
fi
# read getopt’s output this way to handle the quoting right:
eval set -- "$PARSED"
domain=devdomain
user="$(whoami)"
key=id_rsa
# now enjoy the options in order and nicely split until we see --
while true; do
case "$1" in
-u|--user)
user="$2"
shift 2
;;
-k|--key)
key="$2".pem
shift 2
;;
-p|--prod)
domain=proddomain
shift
;;
-s|--server)
server="$2"
shift 2
;;
--)
shift
break
;;
*)
echo "Programming error"
exit 3
;;
esac
done
if [ -z "$server" ]; then
echo "$0: A server name (-s|--server) is required."
kill -INT $$
fi
echo "ssh -i ~/.ssh/$key.pem $user@$server.$domain.com"
ssh -i ~/.ssh/$key $user@$server.$domain.com
}
【问题讨论】: