2015-08-13 37 views
1

我正在处理一个小脚本,我遇到的问题的一部分是找到正确的文件来处理。该脚本设置工作目录,并提示用户输入文件名,如下所示:如何在bash/unix脚本中检测用户输入?

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

我如何读取用户的输入(甚至可以让用户输入的东西,然后读取它),那么这将是用于在同一目录中打开文件?

我会使用一个变量,并将用户的输入分配给它或不同的东西?

谢谢!

+0

在看看:'帮助-m阅读| less' – Cyrus

+0

谢谢@Cyrus,你介意解释一下每个部分的含义,以便我能理解它吗? –

+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 
+0

嗨@Jens,谢谢你的回复。 -n用于读取行数(或仅用于后面的行数)来设置变量名称/性别? –

+0

我认为@Jens有点不对。 '-n'参数指定了一些要读取的_characters_。在这个例子中,按'm'或'f','read'会立即终止。你不必按[ENTER]键。 – rojomoke

+0

@rojomoke这只是一个例子 – Jens

相关问题