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

时间:2021-07-08 11:29:18

This question already has an answer here:

这个问题在这里已有答案:

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

2 个解决方案

#1


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

正如其他人所指出的那样,在bash(或其他类似POSIX的shell)中没有任何转向 - 其他更灵活的流控制结构取而代之。在man bash中查找标题复合命令。

In your case, the select command 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

#2


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

这是一个使用选择循环来实现目标的简短示例。如果您想要自定义格式,可以使用带有自定义菜单的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

#1


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

正如其他人所指出的那样,在bash(或其他类似POSIX的shell)中没有任何转向 - 其他更灵活的流控制结构取而代之。在man bash中查找标题复合命令。

In your case, the select command 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

#2


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

这是一个使用选择循环来实现目标的简短示例。如果您想要自定义格式,可以使用带有自定义菜单的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