3

This question is similar to this one: https://serverfault.com/questions/342697/prevent-sudo-apt-get-etc-from-swallowing-pasted-input-to-stdin but the answer is not satisfying (appending && to each line of bash script is not elegant) and does not explain why some users can paste/execute multiple subsequent apt-get install -y commands and others can't because stdout is swollen by the next command.

I have a script my_script.sh:

sudo apt-get install -y graphicsmagick
sudo apt-get install -y libgraphicsmagick++1-dev
...

It can have only two lines or more of sudo apt-get install stuff. The libraries (graphicsmagick, etc.) doesn't matter, it can be any library.

When I copy this script and paste it's contents to bash or just execute it like this:

cat my_script.sh | sudo -i bash

then for some reason only the first line (graphicsmagick) gets executed and the rest is just printed to the console. It happens only with sudo apt-get install -y, other scripts, which doesn't contain this command behave normally.

If I change bash to sh (which is dash) I get expected behaviour:

cat my_script.sh | sudo -i sh

Can you explain why this happens?

When answering, can you please avoid this questions/comments:

  1. Why are you doing it this way?
  2. Piping to your bash is not safe
  3. Some other aspects are not safe or hackish

I just want to know why bash doesn't work as I would expect and sh does.

PS. I'm using Ubuntu 14.04, sh is dash as you can see here:

vagrant@vagrant-ubuntu-trusty-64:/tmp$ ls -l /bin/sh
lrwxrwxrwx 1 root root 4 Feb 19  2014 /bin/sh -> dash
12
  • How do you know sh is dash? ls -l /bin/sh should tell you. What OS are you using? Commented Oct 6, 2014 at 21:50
  • @KeithThompson - yes, it's dash, ubuntu 14.04, quiestion edited. Commented Oct 6, 2014 at 22:12
  • Probably not relevant to your problem, but you shouldn't need to use sudo both in your script and when invoking bash or sh. Commented Oct 6, 2014 at 22:19
  • @KeithThompson - agree but should it cause any problems? Commented Oct 6, 2014 at 22:49
  • 2
    This could have been reduced to a script file sudo apt-get -y install graphicsmagick<NEWLINE>sudo apt-get -y install libgraphicsmagick++1-dev getting executed differently by cat tmp.sh | bash and cat tmp.sh | dash. This is what a minimal example is about, and doesn't require people to either sudo-execute a downloaded script or reducing the problem themselves. Verified this on Mint 17; it seems like bash is somehow feeding the rest of stdin to apt-get before the -y option kicks in. Strange... Commented Oct 7, 2014 at 11:30

2 Answers 2

1

Bash and dash simply behave different when using -i flag. Bash always goes to interactive mode even when stdin is not a terminal. Dash on the other hand will not go into interactive mode, even with -i flag.

Sign up to request clarification or add additional context in comments.

Comments

0

Probably need the -s option

If the -s option is present, or if no arguments remain after option processing, then commands are read from the standard input. This option allows the positional parameters to be set when invoking an interactive shell.

Bash man page

curl -s http://foo.com/bar.sh | sudo -i bash -s

Example

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.