Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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?

share|improve this question
add comment

3 Answers 3

up vote 0 down vote accepted

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

share|improve this answer
add comment

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.

share|improve this answer
add comment

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.

share|improve this answer
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 Kath Nov 9 '13 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 '13 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 Kath Nov 9 '13 at 20:46
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.