【问题标题】:How to detect user input in bash/unix scripting?如何检测 bash/unix 脚本中的用户输入?
【发布时间】:2015-11-06 23:18:18
【问题描述】:

我正在编写一个小脚本,我遇到的部分问题是找到要使用的正确文件。该脚本设置工作目录并提示用户输入文件名,如下所示:

#!/bin/bash
#Now to set the directory for the user files, which will be used to execute the$
#cd "$(M:/ "$0")" // to be used on campus machines only
#using it at home will break the script due to not being on the M:/ drive
echo We are currently working from the M directory.
echo Please enter the bash script name, which should be formatting as username.

我如何读取用户的输入(甚至允许用户输入一些内容然后读取),然后这些输入将用于打开同一目录中的文件?

我会使用变量并将用户的输入分配给它还是其他的东西?

谢谢!

【问题讨论】:

  • 看看:help -m read | less
  • 谢谢@Cyrus,您介意解释一下每个部分的含义,以便我能正确理解吗?
  • 哎呀,我是个白痴。谢谢。

标签: bash unix scripting


【解决方案1】:

你可以使用read函数

echo -n "Enter your name and press [ENTER]: "
read name
echo -n "Enter your gender and press [ENTER]: "
read -n 1 gender
echo

【讨论】:

  • 嘿@Jens,感谢您的回复。 -n 是否用于读取行数(或仅读取之后的行数)来设置变量名称/性别?
  • 我认为@Jens 有点不对劲。 -n 参数指定要读取的字符 的数量。在本例中,您按下 mfread 将立即终止。您不必按[ENTER]
  • @rojomoke 这只是一个例子
猜你喜欢
  • 1970-01-01
  • 2016-11-18
  • 1970-01-01
  • 2023-03-04
  • 2013-01-18
  • 2011-03-13
  • 1970-01-01
  • 2021-12-16
  • 1970-01-01
相关资源
最近更新 更多