[colug-432] bash pipe + read?
Rick Hornsby
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[1] 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"[2], but I don't understand the answers.
A simple example[3] showing the difference:
./pipe_test.sh
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
thanks!
[1] http://rvm.io <http://rvm.io/>
[2] 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>
[3] https://gist.github.com/rjhornsby/dcdaff555d26a00a27b2
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.colug.net/pipermail/colug-432/attachments/20151116/8fdc3d83/attachment.html
More information about the colug-432
mailing list