0

I have a here document inside a bash script. I want to read a value from it like that :

su myUser<<SESSION
set -x 
echo -n "Enter your name and press [ENTER]: "
read name
echo "the name is $name"
SESSION

But when I launch this script from another user, bash does not stop himself to wait for an input and ignore the read command.

Any ideas ?

2

As L. Scott Johnson correctly found, the read reads from standard input. The standard input in the shell that su runs is connected to the here-document, so the read reads the literal string echo "the name is " (note that since the here-document is unquoted, the $name has already been expanded to an empty string, or to whatever value it had in the calling shell).

Here is the same thing, but with a quoted here-document, and an extra line outputting $name again:

su username <<'SESSION'
set -x
echo -n "Enter your name and press [ENTER]: "
read name
echo "the name is $name"
echo "What I read was $name"
SESSION

The output would be

Password:
+ echo -n Enter your name and press [ENTER]:
Enter your name and press [ENTER]: + read name
+ echo What I read was echo "the name is $name"
What I read was echo "the name is $name"

To correctly do this, you can't have read reading from standard input. Instead, open a new file descriptor as a copy of standard input, and get read to read from that:

su username 3<&0 <<'SESSION'
set -x
echo -n "Enter your name and press [ENTER]: "
read name <&3
echo "the name is $name"
SESSION

If the shell of the other user is bash or ksh, then read name <&3 may be replaced by read -u3 name.

Note however that you can't expect the name variable to be set in the shell calling su as a child shell can't modify the environment (variables etc.) of a parent shell.

1

It doesn't ignore the read command; it executes it and reads stdin. Then the line echo "the name is $name is read (from stdin) and assigned to $name.

su myUser<<SESSION
set -x 
echo -n \"Enter your name and press [ENTER]: \"
read name
someUser
echo \"the name is \$name\"
SESSION

output:

+ echo -n '"Enter' your name and press '[ENTER]:' '"'
-n "Enter your name and press [ENTER]: "
+ read name
+ echo '"the' name is 'someUser"' 
"the name is someUser"
  • You're right, I understand now :) – Couim Jun 12 at 14:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.