bash 如何在shell脚本中使用goto语句

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30182086/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 21:58:21  来源:igfitidea点击:

How to use goto statement in shell script

bashshell

提问by RAHUL.S. KRISHNA

I am beginner in shell script. I don't have any idea about how to use goto statement. I am using the following code.

我是 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

采纳答案by mklement0

As others have noted, there's no gotoin bash(or other POSIX-like shells) - other, more flexible flow-control constructs take its place.
Look for heading Compound Commandsin man bash.

正如其他人所指出的,没有gotoin bash(或其他类似 POSIX 的 shell)——其他更灵活的流控制结构取而代之。
寻找标题Compound Commandsman bash

In your case, the selectcommand is the right choice. Since how to use it may not be obvious, here's something to get you started:

在您的情况下,该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

回答by David C. Rankin

Here is a short example using a selectloop to accomplish your goal. You can use a whileloop with a custom menu if you want custom formatting, but the basic menu is what selectwas designed to do:

这是一个使用select循环来实现目标的简短示例。while如果您想要自定义格式,您可以使用带有自定义菜单的循环,但基本菜单的select设计目的是:

#!/bin/bash

## array of menu entries
entries=( "for Copy"
          "for exit" )

## set prompt for select menu
PS3='Selection: '

while [ "$menu" != 1 ]; do                ## outer loop redraws menu each time
    printf "\nMain Menu:\n\n"             ## heading for menu
    select choice in "${entries[@]}"; do  ## select displays choices in array
        case "$choice" in                 ## case responds to choice
            "for Copy" )
                echo "CopyNUM"
                break                     ## break returns control to outer loop
                ;;
            "for exit" )         
                echo "Haiiii, exiting"
                menu=1                    ## variable setting exit condition
                break
                ;;
            * )
                echo "ssss"
                break
                ;;
        esac
    done
done

exit 0

Use/Output

使用/输出

$ bash select_menu.sh

Main Menu:

1) for Copy
2) for exit
Selection: 1
CopyNUM

Main Menu:

1) for Copy
2) for exit
Selection: 3
ssss

Main Menu:

1) for Copy
2) for exit
Selection: 2
Haiiii, exiting