简体   繁体   English

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

[英]How to detect user input in bash/unix scripting?

I'm working on a small script and part of the problem I'm having is finding the correct file to work with. 我正在处理一个小的脚本,部分问题是找到可以使用的正确文件。 The script sets the working directory and prompts the user to enter the file name like so: 该脚本设置工作目录并提示用户输入文件名,如下所示:

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

How do I read the user's input (or even allow the user to input something and then read it) which will then be used to open a file in the same directory? 如何读取用户的输入(或什至允许用户输入某些内容然后阅读),然后将其用于打开同一目录中的文件?

Will I be using a variable and assigning the user's input to it or something different? 我会使用变量并将用户输入分配给它还是其他一些东西?

Thanks! 谢谢!

You can use the read function 您可以使用read功能

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM