1

I'm writing a shell script that asks for a value and depending on the valued entered returns a value. Imagine that it's called "reve" and contains something like

read fname
rev << EOF 
$fname
EOF

Then if I have a file that is called "file.txt" and I do

vi `./reve`

Then waits for user input. If I type "txt.elif" vi opens "file.txt". Correct until now. But the problem is when the script is something like the following

echo "Enter inverted file name"
read fname
rev << EOF 
$fname
EOF

Then it tries to open a file called "Enter inverted file name".

Is it possible to ask for the value with the text and after that use the returned value only?

Thanks in advance.

3 Answers 3

3

If you really need it to be interactive, then you could print the message on stderr instead.

echo "Enter inverted file name" > /dev/stderr
read fname
rev << EOF 
$fname
EOF
Sign up to request clarification or add additional context in comments.

Comments

1

Unless you're using a really old shell, read should have a -p option to supply a prompt. It automatically sends the prompt to stderr instead of stdout, and skips the linefeed (so the response is on the same line as the prompt):

read -p "Enter inverted file name: " fname
rev << EOF 
$fname
EOF

Comments

1

Force writing to, and reading from, the terminal using /dev/tty.

In your example this will be:

echo -e "Enter inverted file name: \c" > /dev/tty
read fname < /dev/tty
rev << EOF 
$fname
EOF

I used echo -e ... \c so no newline is printed and your input is entered on the same line.

Comments

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.