2

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!

3
  • Take a look at: help -m read | less Commented Aug 13, 2015 at 20:14
  • Thanks @Cyrus, would you mind explaining what each part means so I can understand it properly please? Commented Aug 13, 2015 at 20:16
  • Whoops, I'm an idiot. Thanks. Commented Aug 13, 2015 at 20:26

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

3 Comments

Hey @Jens, thanks for replying. Is -n used to read the amount of lines (or just the one afterwards) to set the variable name/gender?
I think @Jens has it slightly wrong. The -n parameter specifies a number of characters to read. In this example you'd press m or f and the read would immediately terminate. You wouldn't have to press [ENTER].
@rojomoke It is only an example

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.