简体   繁体   中英

read gets skipped in here-document

I have a script test1.sh. It has rwx permissions for all users. It contains this text :

echo "You are in test1.sh"
read -p "Enter some text : " text1
echo "You entered : $text1"
bash <<!
        echo "You are now in a here-document"
        read -p "Enter some more text : " text2
        echo "You also entered : $text2"
        echo "End of the here-document"
!
echo "End of test1.sh"

When I run it as sh test1.sh, the second read statement is skipped completely. Here is the output :

$ sh test1.sh
You are in test1.sh
Enter some text : hello
You entered : hello
You are now in a here-document
End of the here-document
End of test1.sh

When I run it as ./test1.sh, both statements are skipped.

$ ./test1.sh
You are in test1.sh
./test1.sh[11]: read: no query process
You entered :
You are now in a here-document
End of the here-document
End of test1.sh

If I change test1.sh to use /dev/tty,

echo "You are in test1.sh"
read -p "Enter some text : " text1
echo "You entered : $text1"
bash <<!
        echo "You are now in a here-document" > /dev/tty
        read -p "Enter some more text : " text2 < /dev/tty
        echo "You also entered : $text2" > /dev/tty
        echo "End of the here-document" > /dev/tty
!
echo "End of test1.sh"

then I get :

$ sh test1.sh
You are in test1.sh
Enter some text : hello
You entered : hello
You are now in a here-document
Enter some more text : bye
You also entered :
End of the here-document
End of test1.sh

The second text didn't get printed.

If I edit the file to use stdin and stdout,

echo "You are in test1.sh"
read -p "Enter some text : " text1
echo "You entered : $text1"
bash <<!
        echo "You are now in a here-document" > /dev/stdout
        read -p "Enter some more text : " text2 < /dev/stdin
        echo "You also entered : $text2" > /dev/stdout
        echo "End of the here-document" > /dev/stdout
!
echo "End of test1.sh"

, I get this output :

$ sh test1.sh
You are in test1.sh
Enter some text : hello
You entered : hello
You are now in a here-document
You also entered :
End of the here-document
End of test1.sh

I couldn't input any text for the second read statement

I want to read input from each read statement into text1 and text2 respectively and I want both $text1 and $text2 to be echo-ed. Could you please tell me what is wrong?

When you're processing the here-document, standard input is the rest of the here-document. So in the first script, the second read line is reading the echo line that comes after it. Since that line is being read by read , it doesn't get executed by the shell.

I think the reason for the error in your second script is because you didn't put #!/bin/bash at the beginning of the script. Without that, the script is executed by sh instead of bash , and it doesn't understand the -p option to read .

In the third script, the reason $text2 doesn't get echoed is because variables in a here-document are expanded by the shell reading the here-document. But the variable $text2 is set in the child shell process. You can solve this with <<'!' -- putting quotes around the end-marker means that the here-document should be treated as a literal string, and variables should not be expanded.

In the last script, redirecting input from /dev/stdin doesn't do anything, because that's where input is already coming from.

Consider the first script:

echo "You are in test1.sh"
read -p "Enter some text : " text1
echo "You entered : $text1"
bash <<!
        echo "You are now in a here-document"
        read -p "Enter some more text : " text2
        echo "You also entered : $text2"
        echo "End of the here-document"
!
echo "End of test1.sh"

Where is the standard input of the bash coming from? Answer: the here document. So the read tries to read from standard input too. The chances are that the shell has read everything so the read comes up empty.

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