[colug-432] bash pipe + read?
richardjhornsby at gmail.com
Mon Nov 16 10:38:15 EST 2015
I've written a shell script that's meant to be run similar to how you install rvm using curl:
$ curl http://somehost/myscript.sh <http://somehost/myscript.sh> | sudo bash -s devel
It's been working great for a couple of months, but I ran into a problem when I tried to add a read prompt. Normally, I don't like doing read statements in a shell script. In this case, I'm using it as a way to allow the user to press enter to skip a timer and reboot the host immediately. If the user does nothing, the host will reboot after 2 minutes. I've also trapped SIGINT so they can ctrl-c to cancel the reboot.
I think I understand the problem - what's happening is that because there's an input pipe to bash, read takes the next line of the script as its input instead of taking it from the user - they're both on stdin. There's what appears to be an answer "Inverting the Fork", but I don't understand the answers.
A simple example showing the difference:
cat pipe_test.sh | bash
My script is a couple of hundred lines long, and the only part I'm having an issue with is at the very end. I say that because it's been suggested to contain the entire contents of the script in a HEREDOC - that seems way excessive.
Alternatively, I could make the user do
$ wget http://somehost/myscript.sh <http://somehost/myscript.sh>; chmod 755 myscript.sh; ./myscript.sh
But that seems far less elegant than curl ... | bash
 http://rvm.io <http://rvm.io/>
 http://stackoverflow.com/questions/13763942/bash-why-piping-input-to-read-only-works-when-fed-into-while-read-const <http://stackoverflow.com/questions/13763942/bash-why-piping-input-to-read-only-works-when-fed-into-while-read-const>
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the colug-432