返回到bash脚本中以前的命令?
我正在尝试在bash脚本中实现一个返回到bash脚本中以前的命令?,bash,macos,shell,Bash,Macos,Shell,我正在尝试在bash脚本中实现一个prev选项,以返回到上一个“菜单”,以及一种在$name未设置变量时脚本再次请求用户输入的方法 下面是我的bash脚本: #!/bin/bash #Menu() { for (( ; ; )) do beginORload= echo "Choose option:" echo "1 - Begin" echo "2 - Load" read -p "?" beginORload #} #Begin() { if [ "$begin
prev
选项,以返回到上一个“菜单”,以及一种在$name
未设置变量时脚本再次请求用户输入的方法
下面是我的bash脚本:
#!/bin/bash
#Menu() {
for (( ; ; ))
do
beginORload=
echo "Choose option:"
echo "1 - Begin"
echo "2 - Load"
read -p "?" beginORload
#}
#Begin() {
if [ "$beginORload" -eq "1" ]
then
clear
for (( ; ; ))
do
echo "Beginning. What is your name?"
read -p "?" name
#If "prev" specified, go back to #Menu()
if [ "$name" -eq "prev" ]
then
Menu
fi
#If nothing specified, return to name input
if [ -z ${name+x} ]
then
Begin
else
break
fi
echo "Hi $name !"
done
fi
done
在批处理中,我可以简单地执行以下操作:
:menu
echo Choose option:
echo 1 - Begin
echo 2 - Load
[...]
:begin
[...]
if "%name%==prev" goto menu
if "%name%==" goto begin
问题是我到处都会遇到错误,我不知道该键入什么才能让它工作
我经营约塞米蒂顺便说一句。谢谢你像这样的事情接近你的期望:
while [[ $answer -ne '3' ]];do
echo "Choose option:"
echo "1 - Begin"
echo "2 - Load"
echo "3 - Exit"
read -p "Enter Answer [1-2-3]:" answer
case "$answer" in
1) while [[ "$nm" == '' ]];do read -p "What is your Name:" nm;done # Keep asking for a name if the name is empty == ''
if [[ $nm == "prev" ]];then nm=""; else echo "Hello $nm" && break; fi # break command breaks the while wrapper loop
;;
2) echo 'Load' ;;
3) echo 'exiting...' ;; # Number 3 causes while to quit.
*) echo "invalid selection - try again";; # Selection out of 1-2-3 , menu reloaded
esac # case closing
done # while closing
echo "Bye Bye!"
一般来说,您可以在while循环中结束您的案例
选择,该循环在某些情况下会中断(即,如果选择了选项3或提供了有效名称(非空-非上一个)
PS1:在bash中,您将整数与-eq
、-ne
等进行比较,但将字符串与=
或!=
进行比较
PS2:检查上面的代码你只需要使用
案例
或选择
-请看这里的第6章:谢谢!我会试试这个。我还在努力让它工作。还有什么帮助吗?嗨。我很快就要完成我的脚本了!我只需要更多的帮助。我还试图添加更多的菜单和更多的“prev”选项。例如,我想(我把代码放在pastebin上,这样看起来比在注释中更好。)^ ^我该怎么做呢?