1

This is slightly messy to explain.

I have a script that I want to deploy like this:

curl -Ls example.com/myscript.sh | bash

The trouble is the script reads from standard input like so:

echo "You should edit /srv/pillar/tm.sls now. Comments are in the file."
echo Type "skip" to skip or anything else to edit.
read text
if [ "$text" != "skip" ]; then
sudo pico /srv/pillar/tm.sls
fi

Now, since stdin is obviously serving as the source of the script itself, this doesn't currently work. Is there a way I can regain the terminal?

(The current workaround is to deploy it like this: curl -Ls example.com/myscript.sh > myscript.sh && bash myscript.sh)

2
  • don't force users to use pico. use editor=${VISUAL:-$EDITOR}; sudo "${editor:-pico}" file Feb 19, 2014 at 14:26
  • Thanks for the tip - I looked for a variable like that, but neither VISUAL nor EDITOR were set. Also didn't know about {$VAR:-default} substitution, thanks again. Feb 19, 2014 at 22:40

1 Answer 1

2

Use a process substitution

bash <(curl -Ls example.com/myscript.sh)
4
  • Thanks - I still don't understand from the Gnu page how that's different from piping, but it does work. It's better than my current workaround, but I was still hoping for a way in the form curl ... | bash, because I think that's what people expect. Feb 19, 2014 at 22:47
  • @SteveBennett this is different from piping in that this invokes bash filename where filename is the name of the FIFO to which the curl process writes its output. Piping is not possible by definition: it connects a stream to bash's stdin.
    – zwets
    Feb 20, 2014 at 9:59
  • Piping has a side-effect: the components of a pipeline are executed in subshells. The difficulty is seen in something like this: seq 10 | while read num; do ((sum+=num)); done; echo $sum -- $sum is blank because the while loop is executed in a subshell, and all the modifications to the "sum" variable disappear when the subshell exits. Feb 20, 2014 at 12:10
  • Yeah, thanks. Interesting that I've never seen this technique before! Feb 21, 2014 at 0:09

You must log in to answer this question.

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