4

I keep some bash snippets and copy&paste them when I needed for management. But I discovered apt-get cancels script execution. Here's my script where problematic.

apt-get -y install gcc g++ make cmake perl

cd ~/
mkdir t1
cd t1

I copy & paste this script on OS X Terminal to Ubuntu 12.04 LTS server (fresh install on VM) Script always stop after apt-get finished.

I run this command with root account like this.

ssh user1@server

<password…>

sudo su

<password…>

apt-get -y install gcc g++ make cmake perl

cd ~/
mkdir t1
cd t1

Can this be a problem? Or why my script stops after apt-get finished, and how to make it to continue?

4 Answers 4

10

I had the same problem but was not very happy with the solutions here or the solutions did not work for me. What I found out to get it running is to send a NULL input to apt-get, so that it continues to work.

It looks like this:

apt-get -y install gcc g++ make cmake perl  < "/dev/null"

cd ~/
mkdir t1
cd t1

#newline

Hope other people can use this too! Don't forget the newline so that the last command will also be executed

1
  • This helped me so much. Was trying to create a gist shell script to install and setup common programs and environment settings that I could pipe into bash. It kept echoing out the commands after the apt-get isntall and i couldn't figure out why. Thank you! Sep 19, 2014 at 22:12
3

First off, you're not running a script. Pasting a series of commands doesn't make a script. As described in Jobin's answer, you would not have this problem if you were actually using a script.

The problem is probably that you're overloading the input buffer with the commands and hoping that bash is the next process to read from the input buffer. There are a lot of reasons that apt-get would read from the input buffer instead, so nothing is left when it finishes and drops you back to bash.

If you want, you can still paste a series of commands without going to the trouble of copying or writing a script file and making it executable. (Creating the script would be a waste of effort in many cases, for example a simple set of commands that you have to do on multiple machines and never twice on the same machine.)

If you paste the series of commands separated by semicolons, the whole sequence gets fed to bash and will continue regardless of what apt does with the input buffer:

apt-get -y install gcc g++ make cmake perl; cd ~/; mkdir t1; cd t1

2

You shouldn't be copy-pasting the commands in the script on the terminal, the script should be executed on the terminal. The problem with the first approach is, as soon as the first command get pasted along with the new-line at the end of it, apt-get begins executing which prevents further commands from being pasted on the terminal, so they won't be executing.

To execute a script, write the commands to a file run.sh(or any name you want) as follows:

#!/bin/bash
apt-get -y install gcc g++ make cmake perl

cd ~/
mkdir t1
cd t1

and then to execute it on a terminal, type bash run.sh or bash filename (replace filename by the name you have given to the script). The first line of this script tells which shell to use in case you directly run the script(by first making it executable using chmod +x filename) and then running the script as /path/filename.

0
0

apt-get sometimes exits with non-zero code even it installed successfully.

And that makes script to halt. This is nonsense and ridiculous but I have to workaround this. I tried to force it to continue by wrapping apt-get block with sub-script so exit code to be ignored.

set -e

bash << +END
apt-get -y install gcc g++ make cmake perl
exit 0
+END
echo $?

cd ~/
mkdir t1
cd t1

Actually I expected the inside bash would exit with non-zero code - so outside script would quit cascade - but it didn't and execution continued.

3
  • 1
    If you're using set -e and you want to ignore the exit code for one command, it's a lot simpler to use the form apt-get -y install gcc g++ make cmake perl || true
    – Steven K
    Nov 9, 2013 at 20:30
  • @StevenKath I want my script to continue only when all the packages are ready. Anyway this was the best I could figure out.
    – Eonil
    Nov 9, 2013 at 20:36
  • This way, it will continue only when apt-get is complete. || true uses the bash || operator (only run the following if the previous exit code was non-zero) followed by true (exit with code 0). Together, they simply turn a non-zero exit code into a zero (success) exit code.
    – Steven K
    Nov 9, 2013 at 20:46

You must log in to answer this question.

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