How do I get my shell script to automatically accept prompts?

6,327

Solution 1

Use magic.

If you're low on magic, try passing apt-get some arguments. -y should help you out. It tells apt-get in advance that you're going to say yes to the confirmation prompt.

apt-get install -y docky geany firefox audacity kdenlive openshot shotwell stellarium thunderbird

For a list of what you can and can't do with apt-get, type man apt-get in a terminal and hit Enter. You can do this with any other command too. For example, man sudo, or man nano, or even man chromium-browser.

Solution 2

You can simply list multiple packages to install in the same command. I believe this will also give you only one y/n prompt. Example:

sudo apt-get install docky geany firefox

Solution 3

I've found in the past that stringing the commands together in that manner causes issues if one of them have a "hiccup".

For example, if your command is similar to this:

sudo apt-get install 1 2 3 4

then, if there's an error on program 3, 4 will not install. Keeping them separate on each line fixes this issue. if there's an error, the script continues on.

Here's an example of a script I use for new installs:

echo " "
echo "Please run this in an empty folder. Failure to do so may cause data loss!"
read -p "Press ENTER to continue, or close the window to abort."

echo " "
echo "Now backing up the sources list."
read -p "Press ENTER to continue..."
sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak

echo " "
echo "Now adding all the new repositories and keys."
read -p "Press ENTER to continue..."
sudo add-apt-repository -y "ppa:maarten-baert/simplescreenrecorder"
sudo add-apt-repository -y "deb http://download.videolan.org/pub/debian/stable/ /"
sudo add-apt-repository -y "ppa:stebbins/handbrake-releases"
sudo add-apt-repository -y "ppa:tsbarnes/indicator-keylock"
sudo add-apt-repository -y "ppa:freefilesync/ffs"
sudo add-apt-repository -y "deb http://apt.insynchq.com/ubuntu trusty non-free contrib"
sudo apt-add-repository -y "ppa:rael-gc/scudcloud"
wget -qO - https://d2t3ff60b2tol4.cloudfront.net/[email protected] \ | sudo apt-key add -
wget -O - http://download.videolan.org/pub/debian/videolan-apt.asc | sudo apt-key add -

echo " "
echo "Now removing all the programs I never use"
read -p "Press ENTER to continue..."
sudo apt-get autoremove -y abiword 
sudo apt-get autoremove -y gnumeric 
sudo apt-get autoremove -y firefox 
sudo apt-get autoremove -y gmusicbrowser 
sudo apt-get autoremove -y parole
sudo apt-get update

echo " "
echo "Now installing everything I use."
read -p "Press ENTER to continue..."
sudo apt-get install -y gdebi
sudo apt-get install -y screen
sudo apt-get install -y openvpn
sudo apt-get install -y openjdk-7-jre
sudo apt-get install -y guake
sudo apt-get install -y libreoffice
sudo apt-get install -y audacity
sudo apt-get install -y synaptic
sudo apt-get install -y moc
sudo apt-get install -y p7zip-full
sudo apt-get install -y mtools
sudo apt-get install -y mono-runtime
sudo apt-get install -y wine
sudo apt-get install -y aisleriot
sudo apt-get install -y gparted
sudo apt-get install -y tango-icon-theme
sudo apt-get install -y tango-icon-theme-extras
sudo apt-get install -y thunderbird
sudo apt-get install -y htop
sudo apt-get install -y xscreensaver
sudo apt-get install -y python-pygame
sudo apt-get install -y gimp
sudo apt-get install -y xterm
sudo apt-get install -y indicator-keylock 
sudo apt-get install -y libdvdcss2 
sudo apt-get install -y vlc 
sudo apt-get install -y handbrake-gtk 
sudo apt-get install -y openshot 
sudo apt-get install -y openshot-doc 
sudo apt-get install -y simplescreenrecorder
sudo apt-get install -y freefilesync 
sudo apt-get install -y mono-devel 
sudo apt-get install -y pinta 
sudo apt-get install -y insync
sudo apt-get install -y scudcloud
sudo apt-get install -y hunspell-en-ussudo 
sudo apt-get install -y xscreensaver-screensaver-webcollage 
sudo apt-get install -y xscreensaver-screensaver-bsod 
sudo apt-get install -y xscreensaver-screensaver-dizzy 
sudo apt-get install -y xscreensaver-gl 
sudo apt-get install -y xscreensaver-gl-extra 

echo " "
echo "Downloading .deb files."
read -p "Press ENTER to continue..."
wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
wget http://download.teamviewer.com/download/teamviewer_i386.deb

echo " "
echo "Now installing the .deb files we downloaded earlier."
read -p "Press ENTER to continue..."
sudo gdebi google-chrome-stable_current_amd64.deb
sudo gdebi teamviewer_i386.deb

echo " "
echo "Now configuring mono."
read -p "Press ENTER to continue..."
mozroots --import --ask-remove

echo " "
echo "Now cleaning up the mess we made."
read -p "Press ENTER to continue..."

echo " "
sudo apt-get autoremove -y && sudo apt-get clean

echo " "
echo "Now we'll download kernel 4.0. ABORT if you are uncertain that you want this!"
read -p "Press ENTER to continue..."
wget http://kernel.ubuntu.com/~kernel-ppa/mainline/v4.0-rc1-vivid/linux-headers-4.0.0-040000rc1_4.0.0-040000rc1.201502222235_all.deb
wget http://kernel.ubuntu.com/~kernel-ppa/mainline/v4.0-rc1-vivid/linux-headers-4.0.0-040000rc1-generic_4.0.0-040000rc1.201502222235_amd64.deb
wget http://kernel.ubuntu.com/~kernel-ppa/mainline/v4.0-rc1-vivid/linux-image-4.0.0-040000rc1-generic_4.0.0-040000rc1.201502222235_amd64.deb

echo " "
echo "Now installing kernel 4.0. LAST CHANCE TO ABORT!"
read -p "Press ENTER to continue..."
sudo dpkg -i linux-headers-4.0*.deb linux-image-4.0*.deb

echo " "
echo "Now cleaning up our mess again."
read -p "Press ENTER to continue..."
rm *.deb
sudo reboot
Share:
6,327

Related videos on Youtube

shortstheory
Author by

shortstheory

Updated on September 18, 2022

Comments

  • shortstheory
    shortstheory over 1 year

    I made a simple shell script (that I run as root) to download essential apps that I might need whenever I reinstall ubuntu: It looks like this:

    apt-get install docky && apt-get install geany && apt-get install firefox && apt-get install audacity && apt-get install kdenlive && apt-get install openshot && apt-get install shotwell && apt-get install stellarium && apt-get install thunderbird, etc...
    

    The problem is I still get prompted with Y/n prompts and I manually need to press Y all the time. Is there a way I can automatically accept all the prompts and download and install everything?

    • shortstheory
      shortstheory almost 10 years
      :( suggestions welcome too!
  • shortstheory
    shortstheory almost 10 years
    sheesh I feel so silly for doing it the way I did. Thanks for the help!
  • amanthethy
    amanthethy almost 10 years
    No worries :) Glad to help!