How to prevent script not to stop after apt-get?
Solution 1
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
Solution 2
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
Solution 3
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
.
Related videos on Youtube
Eonil
Current tools. OSX, iOS, FreeBSD. C/C++/Objective-C, Cocoa, Xcode. PostgreSQL. Feel free to fix my grammar if it's wrong. I always appreciate!
Updated on September 18, 2022Comments
-
Eonil over 1 year
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? -
Steven K over 10 yearsIf you're using
set -e
and you want to ignore the exit code for one command, it's a lot simpler to use the formapt-get -y install gcc g++ make cmake perl || true
-
Eonil over 10 years@StevenKath I want my script to continue only when all the packages are ready. Anyway this was the best I could figure out.
-
LordZardeck over 9 yearsThis 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!