简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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