返回bash脚本中的先前命令? [英] Return to previous commands in bash script?

查看:79
本文介绍了返回bash脚本中的先前命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的bash脚本中实现 prev 选项以返回到上一个菜单",以及该脚本在没有变量的情况下再次要求用户输入的方式为 $ name 设置.

这里是我的bash脚本:

 #!/bin/bash#菜单() {为了 (( ; ; ))做beginORload =回显选择选项:"回声"1-开始"回声"2-加载"读-p?"beginORload#}#开始() {如果["$ beginORload" -eq"1"]然后清除为了 (( ; ; ))做回声开始.你叫什么名字?"读-p?"名称#如果指定了"prev",请返回#Menu()如果["$ name" -eq"prev"]然后菜单科幻#如果未指定,返回名称输入如果[-z $ {name + x}]然后开始别的休息科幻回声你好,$ name!"完毕科幻完毕 

批处理中,我可以简单地做到:

 :菜单回声选择选项:回声1-开始echo 2 - 加载[...]:开始[...]如果%name%==上一个"转到菜单如果%name%==" goto开始 

问题是我到处都遇到错误,而且我不知道要输入什么才能使其正常工作

我正在运行优胜美地.谢谢

解决方案

类似于您所期望的事情

在[[$ answer -ne'3']]期间

 回显选择选项:"回声"1-开始"回声"2-加载"回声"3-退出"阅读-p输入答案[1-2-3]:"答案情况为"$ answer"1)while [["$ nm" =='']];读-p你叫什么名字:" nm; done#如果名字为空==继续询问名字如果[[$ nm =="prev"]];然后nm =";否则回显"Hello $ nm"&&休息;fi#break命令中断while包装循环;;2)回显'Load';;3)回显'exiting ...';;#3号导致退出.*)回显无效选择-再试一次";#在1-2-3中选择,重新加载菜单esac#案例结案done # 关闭时回声再见!" 

一般而言,您可以在一个while循环中包装 case 选择,这在某些情况下会中断(例如,如果选择了选项3或给出了一个有效的名称(不是空白-不是上一页)

PS1:在bash中,您将整数与 -eq -ne 等进行比较,但将字符串与 == 进行比较!=

PS2:检查上面的代码在此处在线

I'm trying to implement a prev option in my bash script to go back to the previous "menu", as well as a way for the script to ask for user input again if no variable is set for $name.

Heres my bash script:

#!/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

In batch, I could simply do:

:menu
echo Choose option:
echo 1 - Begin
echo 2 - Load
[...]
:begin
[...]
if "%name%==prev" goto menu
if "%name%==" goto begin

the issue is I keep running into errors all over the place, and I can't figure out what to type to get it to work

im running Yosemite btw. Thankyou

解决方案

Something like this is close to what you expect:

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!"

As a general idea you can wrap up your case selection in a while loop which will break under certain circumstances (i.e If Option 3 is selected or if a valid name is given (not blank - not prev)

PS1: In bash you compare integers with -eq , -ne, etc but you compare strings with == or !=

PS2: Check the above code online here

这篇关于返回bash脚本中的先前命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆