如何在shell脚本中使用goto语句[重复]

2023-12-03

我是 shell 脚本的初学者。我不知道如何使用 goto 语句。我正在使用以下代码。

start:
echo "Main Menu"
echo "1 for Copy"
echo "2 for exit"
read NUM
case $NUM in
"1")
echo "CopyNUM"
goto start:;
"2")         
echo "Haiiii";
goto start:
*)
echo "ssss";
esac

正如其他人指出的那样,没有goto in bash(或其他类似 POSIX 的 shell) - 其他更灵活的流量控制结构取代了它。
寻找标题Compound Commands in man bash.

就你而言,select命令是正确的选择。 由于如何使用它可能并不明显,这里有一些可以帮助您入门的内容:

#!/usr/bin/env bash

echo "Main Menu"

# Define the choices to present to the user, which will be
# presented line by line, prefixed by a sequential number
# (E.g., '1) copy', ...)
choices=( 'copy' 'exit' )

# Present the choices.
# The user chooses by entering the *number* before the desired choice.
select choice in "${choices[@]}"; do

  # If an invalid number was chosen, $choice will be empty.
  # Report an error and prompt again.
  [[ -n $choice ]] || { echo "Invalid choice." >&2; continue; }

  # Examine the choice.
  # Note that it is the choice string itself, not its number
  # that is reported in $choice.
  case $choice in
    copy)
      echo "Copying..."
      # Set flag here, or call function, ...
      ;;
    exit)
      echo "Exiting. "
      exit 0
  esac

  # Getting here means that a valid choice was made,
  # so break out of the select statement and continue below,
  # if desired.
  # Note that without an explicit break (or exit) statement, 
  # bash will continue to prompt.
  break

done
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在shell脚本中使用goto语句[重复] 的相关文章

  • sed(和其他)会损坏非 ASCII 文件吗?

    如果我编写一些操作文件的脚本 例如使用 sed 进行搜索 替换 并且文件可以采用各种字符集 那么文件是否会损坏 我希望替换的文本是 ASCII 并且也仅出现在仅包含 ASCII 的文件中的行上 但其余行包含其他字符集中的字符 如果您的字符集
  • VSCode 集成终端不加载 .bashrc 或 .bash_profile

    我有以下文件来处理 shell 配置 bash profile if f bashrc then source bashrc fi and bashrc configure shell 如果我使用以下命令从命令行打开 VSCodecode
  • 如何在 grep 中每个文件匹配一次?

    是否有任何 grep 选项可以让我控制匹配总数 但在每个文件的第一个匹配处停止 Example 如果我这样做grep ri include coffee re 我明白了 app coffee express require express
  • 如何在 Bash 脚本中回答“是”

    有一个快速的问题 想象一下我有这样的代码 mkdir p INSTALLDIR sudo apt get install y git clojure leiningen git clone git github com maltoe sto
  • 自动接受安装 NPX 包 [重复]

    这个问题在这里已经有答案了 运行 NPM 包时npx第一次会出现提示询问是否要下载包 例如 如果您运行命令npx some npm package 您会收到以下提示 Need to install the following package
  • 比较两个文件的各自字段并以特定格式输出所需的内容

    我正在比较两个文件 food1 txt 文件并比较 food2 txt 文件 如下所示 cat food1 txt pizza 1ea chicken 5ea tooboo 4ea orange 2ea cat food2 txt pizz
  • 具有此处文档重定向的 Makefile 配方

    有谁知道如何在菜谱上使用此处文档重定向 test sh lt
  • 为什么 bash for 循环出错?

    我正在尝试使用 for 循环运行以下代码 但出现语法错误 请帮忙 输入格式 输入的第一行包含一个整数N 表示整数的个数 下一行包含 N 个空格分隔的整数 它们构成数组 A read n sum 0 for i 1 i lt n i do r
  • 如何让 Rscript 在非交互式 bash 模式下返回状态代码

    我正在尝试以 bash 脚本的形式从以非交互方式运行的 Rscript 中获取状态代码 此步骤是较大数据处理周期的一部分 其中涉及 db2 脚本等 所以我在脚本sample sh中有以下内容 Rscript verbose no resto
  • npx:shell-auto-fallback 参数已被删除

    老实说 我不记得我最后在我的机器上安装了什么 我相信它正在酝酿 gatsby cli 无论如何 从昨天早上开始 当我打开新实例或重置终端 打开新选项卡 源 zshrc 等 时 我的终端一直给出以下错误 Last login Tue Nov
  • cron 作业不适用于 xwindow

    我在 crontab 中有以下行 1 xeyes 它不显示任何 xwindow 但相反 1 touch somefile txt 工作正常 尝试在谷歌上搜索但没有得到任何具体答案 如果您运行的命令使用 X 服务器 您必须告诉 cron 在哪
  • 当远程(Http)文件更改时如何执行操作?

    我想创建一个脚本 用于检查 URL 并在远程文件的 Last Modified 标头更改时执行操作 下载 解压缩 我考虑过使用curl 获取标头 但随后我必须将其存储在每个文件的某个位置并执行日期比较 有没有人对使用 大部分 标准 UNIX
  • 如何从 tcl 脚本调用 bash 命令?

    Bash 命令可从交互式 tclsh 会话中获得 例如 在 tclsh 会话中 您可以 ls 代替 exec ls 但是 您不能有一个直接调用 bash 命令的 tcl 脚本 或者没有exec 如何使 tclsh 在解释 tcl 脚本文件时
  • 在詹金斯管道作业中将变量传递给bash脚本

    我有一个 Jenkins 管道作业 其中我使用名为 setup sh 的 bash 脚本配置我的环境 如下所示 bin bash export ARCH 1 echo architecture ARCH 在 Jenkins 管道脚本中 我使
  • 安装heroku toolbelt后出现Ruby错误

    我正在 win 7 32 位系统上使用 Heroku 但我没有管理员权限 我已经下载并安装了heroku工具带如下http community webfaction com questions 11803 heroku toolbelt h
  • 如何在每个 xargs 命令之间休眠 1 秒?

    例如 如果我执行 ps aux awk print 1 xargs I echo 我想让 shell 在每次之间休眠 1 秒echo 如何更改我的 shell 命令 您可以使用以下语法 ps aux awk print 1 xargs I
  • Emacs shell:保存提交消息

    我几天前开始使用 emacs 在 emacs shell M x shell 中使用 git 时遇到问题 当我 git commit 或 git commit amend 时 它会打开 vim 来编辑并保存提交消息 我对此表示同意 但我找不
  • 错误:选项“Z”仅在夜间编译器上被接受[关闭]

    Closed 这个问题需要细节或清晰度 help closed questions 目前不接受答案 我正在通过斯坦福操作系统课程 cs140e https web stanford edu class cs140e 其中一个问题是 有一个名
  • PS1 定义中的条件 pwd

    我想显示当前工作目录如果我处于symlink比没有 到目前为止我已经 pwd P pwd echo 1 31m pwd P echo 1 32m pwd 将返回所需的输出 但它不能替代 w在命令提示符下 我尝试用反引号括起来 但这只会导致p
  • 疯狂的 crond 行为。不断使 bash 进程失效

    我有一个看起来像这样的 crontab SHELL bin bash PATH sbin bin usr sbin usr bin MAILTO root HOME 0 59 var www html private fivemin zda

随机推荐