Linux中如何退出select

发布于 2024-10-22 03:15:54 字数 290 浏览 7 评论 0原文

我想从用户那里获取输入,这可以是任何给定的选项,我尝试使用 select,但它只是循环,没有出来,有什么办法可以使其在用户输入正确的选项后出现并继续,这是示例代码:

select envir in "prod" "uat" "dev"; do
    echo $envir
done

//continue with the script
echo "out of select"

一旦用户选择任何可用选项,它应该出现并继续执行脚本,如果用户输入了其他任何内容,它应该继续提示。

I want to take the input from the user, which could be any of the given options, I tried using select, but it just goes in loop, doesn't come out, is there a way i can make it to come out and proceed after the user has entered right option, here is the sample code:

select envir in "prod" "uat" "dev"; do
    echo $envir
done

//continue with the script
echo "out of select"

Once user selects any of the available options, it should come out and continue with the scripts, if the user has entered anything else, it should keep on prompting.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

童话 2024-10-29 03:15:54

从 bash(1) 手册页:

...列表被执行
每次选择后,直到执行中断命令。 select的退出状态是
列表中执行的最后一个命令的退出状态,如果没有命令被执行则为零
可爱的。 ...

换句话说,当 $envir 非空时执行“break”语句。

From the bash(1) man page:

... The list is executed
after each selection until a break command is executed. The exit status of select is
the exit status of the last command executed in list, or zero if no commands were exe-
cuted. ...

In other words, execute a "break" statement when $envir is nonempty.

仲春光 2024-10-29 03:15:54
select envir in "prod" "uat" "dev"; do
    echo $envir
    if [ $envir == "prod" ] || [ $envir == "uat" ] || [ $envir == "dev" ]
    then
        break
    fi
done

//continue with the script
echo "out of select"
select envir in "prod" "uat" "dev"; do
    echo $envir
    if [ $envir == "prod" ] || [ $envir == "uat" ] || [ $envir == "dev" ]
    then
        break
    fi
done

//continue with the script
echo "out of select"
撞了怀 2024-10-29 03:15:54

谢谢布莱恩。根据您的输入,这就是我能够做的:

select envir in "prod" "uat" "dev"; do
    echo $envir
    if [ "$envir" != "" ]
    then
        break
    fi
done

//continue with the script
echo "out of select"

Thanks Brian. With your input, this is what I was able to do:

select envir in "prod" "uat" "dev"; do
    echo $envir
    if [ "$envir" != "" ]
    then
        break
    fi
done

//continue with the script
echo "out of select"
二智少女猫性小仙女 2024-10-29 03:15:54

我会这样编写上面的脚本:

#!/bin/bash
declare -a opts=("prod" "uat" "dev")

echo "control-D to exit"
select envir in "${opts[@]}"; do
    echo "envir=$envir"
    found=0
    for elem in "${opts[@]}"; do
       if [ "$elem" = "$envir" ]; then
          found=1
          break
       fi
    done
    if [ "$found" -eq 1 ]; then
       break
    fi
done

echo "out of select"

这样您的关键字就可以在一个地方处理。每次在“prod”“uat”“dev”列表中添加新单词时,不需要在2个地方进行更改。

您还可以从外部文件读取单词列表并将其分配给此处的 bash 数组变量 opts。

I would write the above script this way:

#!/bin/bash
declare -a opts=("prod" "uat" "dev")

echo "control-D to exit"
select envir in "${opts[@]}"; do
    echo "envir=$envir"
    found=0
    for elem in "${opts[@]}"; do
       if [ "$elem" = "$envir" ]; then
          found=1
          break
       fi
    done
    if [ "$found" -eq 1 ]; then
       break
    fi
done

echo "out of select"

That way your keywords are handled at one place. Every time you add a new word in the list of "prod" "uat" "dev", you don't need to change at 2 places.

You can also read the list of words from an external file and assign that to bash array variable opts here.

不疑不惑不回忆 2024-10-29 03:15:54

在我看来,这些答案中应该有一个 case 语句的例子,因为将它们与 select 语句配对是很常见的。

PS3="Select and environment: "
select envir in "prod" "uat" "dev"; do
  case $envir in
  prod|uat|dev)
     echo $envir
     break
     ;;
  *)
     echo -e "\nUncrecognized input. Select a number\n"
     ;;
  esac
done
# continue with the script
echo "out of select"

There should be an example of a case-statement among these answers IMO, because it is very common to pair them with select-statements.

PS3="Select and environment: "
select envir in "prod" "uat" "dev"; do
  case $envir in
  prod|uat|dev)
     echo $envir
     break
     ;;
  *)
     echo -e "\nUncrecognized input. Select a number\n"
     ;;
  esac
done
# continue with the script
echo "out of select"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文