我有一个部署到firebase或heroku的脚本。在脚本的最后,如果我用手指额外的键或拼错heroku或firebase,我希望脚本提示我输入所需的主机并使用我想要的输入从顶部再次运行脚本。我尝试在case语句之外放置一个while循环。我希望当脚本到达最终*)
时,它会提示我输入所需的主机,从顶部启动脚本并部署到所需的主机。
对于脚本编写来说还不是新手,我不是百分之百确定这是编写它的最佳方式,但当我使用以下代码运行脚本为deploy heroku
或deploy firebase
时,从字面上看,终端没有任何反应。我试过"$?"
周围的引号并移动了exit 1
,但仍然没有。任何方向将不胜感激。另外,我通过调用
deploy <placetodeploy>
#!/bin/bash
HOST=$1
while [ $? -gt 0 ]; do
case "$HOST" in
heroku)
git push heroku master
;;
firebase)
firebase deploy
;;
*)
read -p "You can only choose between Heroku and Firebase. " HOST; exit 1
;;
esac
done
答案 0 :(得分:2)
试试这个
#!/bin/bash
HOST=$1
while true; do
case "$HOST" in
heroku)
git push heroku master
break
;;
firebase)
firebase deploy
break
;;
*)
read -p "You can only choose between Heroku and Firebase. " HOST
;;
esac
done
这个想法是你有一个永不停止的循环(感谢true
),但是你想要的输入会产生一个break
语句,它会破坏循环的执行,同时捕获 - 所有case语句都允许循环继续。
您可能想要添加&#34;退出/取消&#34;如果你不想让CTRL-C退出循环,可以使用某种选项。
答案 1 :(得分:1)
另一种方法如下:
#!/bin/bash
HOST=$1
while [[ "$HOST" != "heroku" && "$HOST" != "firebase" ]]
do
read -p "You can only choose between heroku and firebase. " HOST
done
case "$HOST" in
heroku)
echo git push heroku master
;;
firebase)
echo firebase deploy
;;
esac
与Fred's answer相比,优点是检查参数和脚本的主体是分开的,而缺点是如果你需要添加更多的情况,你必须在两个地方。