Bash script to setup Docker fails, but individual commands work
<p>I created a bash script to prepare an Ubuntu machine for Docker and install Docker.</p>

<p>However, when running the script, one of the <code>sudo apt-get update</code> commands make the script hang at <code>Reading package lists...</code> for a very very long time (possibly forever). It is the one following the command `apt-cache policy docker-engine'.</p>

<p>The only way past this is to press <kbd>enter</kbd>, which will make the <code>sudo apt-get update</code> continue. </p>

<p>My main concern is that when I run each of these commands manually, they work fine and I do not come across this issue. I only reach this issue when they are ran inside the .sh script. Below is the script:</p>

<pre><code>sudo apt-get update
sudo apt-get install apt-transport-https ca-certificates
sudo apt-key adv --keyserver hkp://p80.pool.sks-keyservers.net:80 --recv-keys 58118E89F3A912897C070ADBF76221572C52609D

echo "deb https://apt.dockerproject.org/repo ubuntu-trusty main" | sudo tee -a /etc/apt/sources.list.d/docker.list

sudo apt-get update
sudo apt-get purge lxc-docker
apt-cache policy docker-engine

sudo apt-get update
echo `sudo apt-get install linux-image-extra-$(uname -r)`

#Install docker
sudo apt-get update
sudo apt-get -y install docker-engine
sudo service docker start
sudo docker run hello-world
</code></pre>

<p>I posted a question regarding the issue with <code>sudo apt-get update</code> and why it hangs on askubuntu: <a href="https://askubuntu.com/questions/796396/reading-package-lists-freezes-until-press-enter">https://askubuntu.com/questions/796396/reading-package-lists-freezes-until-press-enter</a></p>

<p>However, the fact that I get different outcomes when running commands manually vs. when running them in the script makes me think that the issue also belongs on stackoverflow. Is there something wrong with my script that makes one command mess with another command?</p>