如何在bash / unix脚本中检测用户输入?

时间:2015-08-13 20:11:49

标签: bash unix scripting

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

#!/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.

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

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

谢谢!

1 个答案:

答案 0 :(得分: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