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?
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:
Hope other people can use this too! Don't forget the newline so that the last command will also be executed
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
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:and then to execute it on a terminal, type
bash run.sh
orbash 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 usingchmod +x filename
) and then running the script as/path/filename
.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.
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.