1

I'm running the following script on Ubuntu 14.04:

#!/bin/bash

apt-get purge -y nginx
apt-get install -y nginx

date

When I run it like cat /tmp/script | bash, apt-get starts installing, then "date" is printed (not the actual date, but the command name), then the remaining apt-get output is printed.

If however I run the script like /tmp/script, it works as expected: printing the date after apt-get finished.

Why does this happen and how can I force bash to work when being piped to the same way it does when invoked directly?

2
  • I cannot reproduce that (with other commands). What happens if you put echo foo # at the beginning of the apt-get lines? Have a look at the output of od -t c -t x1 /tmp/script. Are there only newlines or maybe \rs, too? Feb 3, 2015 at 9:45
  • @HaukeLaging, with echoes it works as expected. There are no \r symbols. BTW my bash version is 4.3.11 and this is the default ubuntu 14 image provided by AWS
    – Fluffy
    Feb 3, 2015 at 10:05

1 Answer 1

3

You'll have to close (or otherwise redirect) the standard input of the individual commands:

#!/bin/bash

apt-get purge -y nginx <&-
apt-get install -y nginx <&-

date

Otherwise subsequent line are fed to the commands.

3
  • I now get infinite E: Write error - write (14: Bad address) messages
    – Fluffy
    Feb 3, 2015 at 10:47
  • @Fluffy try redirecting in from /dev/null, instead. Oh, wait. "Write error"?
    – muru
    Feb 3, 2015 at 10:49
  • Yes, either way, < /dev/null helped, thanks
    – Fluffy
    Feb 3, 2015 at 11:26

You must log in to answer this question.

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