为什么我的菜单,选择失败的第一次? [英] Why does my menu with select fail the first time?

查看:145
本文介绍了为什么我的菜单,选择失败的第一次?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图回答另一SO使用内建的select语句一个简单的菜单质疑。在code从 / etc / passwd文件显示的名字,让你通过给一些选择名称:

I tried to answer another SO question with a simple menu using the builtin select statement. The code displays names from /etc/passwd, and let you select a name by giving a number:

PS3="Enter a number: "
select name in $(cut -d: -f1 /etc/passwd) ; do
   if [ -n "${name}" ]; then
      break
   fi
   echo "Sorry, please enter a number as shown."
done
echo "Entry from passwd is: ${name}"

除了首次精致的作品。当你给一个正确的答案在第一时间它会问你再试一次。结果
我试图让的第一次更详细的解释,但我不能得到一个可再现的烹调书。当你复制粘贴你的服务器上的此code,并给出一个正确的答案,你可能有同样的问题。当您重复命令(历史或新糊)时,code现在显示的问题。我试图通过注销和登录(有时它的工作原理),或重新启动再次获取问题。结果
我尝试不同的方式来重现在其他情况下的问题(使用不同的变量名,解封的变量,在$使用值的速度慢名单与选择名称(回声之一;睡眠1;回声两种睡眠2;回声三;睡眠1); 键,打开一个新的外壳

我搜索了其他的例子与选择,但我找不到其他职位喜欢的 http://stackoverflow.com/a/16750755/3220113 http://askubuntu.com/a / 1716

I searched for other examples with select, but I can't find clues in other posts like http://stackoverflow.com/a/16750755/3220113 and http://askubuntu.com/a/1716.

我试图解决我的code具有同步和这似乎是一个解决方法:

I tried to fix my code with a sync and that seems to be a work-around:

PS3="Enter a number: "
select name in $(cut -d: -f1 /etc/passwd) ; do
   # is sync needed here?
   sync
   if [ -n "${name}" ]; then
      break
   fi
   echo "Sorry, please enter a number as shown."
done
echo "Entry from passwd is: ${name}"

当我包括sync命令我无法重现的错误。是真正的同步工作的补丁,为什么我在这里这需要?

I couldn't reproduce the error when I include the sync command. Is sync really a working patch and why do I need this here?

我并不需要其他的方式来写一个菜单。我已经找到从bash脚本图形对话框对话并寻找一个简单的替换我自己过于复杂<一个href=\"http://unix.stackexchange.com/a/115371/57293\">http://unix.stackexchange.com/a/115371/57293.

I do not need other ways to write a menu. I already found the graphical dialog Dialog from bash script and was looking for a simple replacement of my own over-complicated http://unix.stackexchange.com/a/115371/57293.

推荐答案

这个问题,只有当您以交互方式输入的命令,而不是在一个脚本发生。其原因是,你在后键入该行选择线被用作响应提示。由于如果未在菜单中,用户报告错误。然后,它不执行如果命令,因为它被读取为响应提示。

This problem only occurs when you type the commands interactively, not in a script. The reason is that the line you type after the select line is being used as the response to the prompt. Since if isn't in the menu, it reports an error. Then it doesn't execute the if command, because it was read as the response to the prompt.

因为在脚本中的命令不作为标准输入这不是一个脚本中的问题。

It's not a problem in a script because the commands in the script are not used as standard input.

这篇关于为什么我的菜单,选择失败的第一次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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