0

I am trying to use read command with wget, for that I am using a simple .sh script:

# echo "Please answer by : -> yes <- or -> no <-"
# read answer
# echo $answer

This code works fine locally, but the read command failed remotely with wget, it finishes without waiting an answer:

# wget -qO - 'https://testserver/pub/test.sh' | bash -x
# + echo 'Please answer by : -> yes <- or -> no <-'
# Please answer by : -> yes <- or -> no <-
# + read answer
#

Thank you for your help.

2
  • 1
    You might want to read from the terminal, not from stdin: read answer < /dev/tty Commented May 14, 2020 at 15:45
  • 1
    You may also run your script as bash -c "$(wget -qO - 'https://testserver/pub/test.sh')". It won't be great, though, since the allowed length of your script will be capped by the command line's max length.
    – fra-san
    Commented May 17, 2020 at 18:29

1 Answer 1

1

When you run your script with bash in the terminal, bash gets your standard input (you only have one) from the keyboard.

keyboard -> script

When you feed the script to bash over a pipe, that pipe becomes the standard input. So your problem is not related to wget, if you did this:

cat test.sh | bash -x

you'd have the same behaviour, because now the input comes from the pipe, not the keyboard. And bash scripts will inherit that standard input.

pipe -> script

As soon as the data in the pipe finishes, so does bash and the script.

A way to solve that is to download, and then run (you don't need the -O but I'm trying to keep your line mostly as-is). Something like this:

$ wget -qO test.sh 'https://testserver/pub/test.sh' && bash test.sh
1
  • Tnxs man for the explanation, you solved my problem.
    – Med
    Commented May 14, 2020 at 15:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .