如何让我的 shell 脚本自动接受提示?

如何让我的 shell 脚本自动接受提示?

我制作了一个简单的 shell 脚本(以 root 身份运行)来下载每次重新安装 ubuntu 时可能需要的基本应用程序:它看起来像这样:

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...

问题是我仍然会收到 Y/n 提示,并且我需要一直手动按 Y。有没有办法可以自动接受所有提示并下载和安装所有内容?

答案1

使用魔法。

如果您缺少魔法,请尝试向 apt-get 传递一些参数。-y应该会有所帮助。它会提前告诉 apt-get 您将对确认提示说“是”。

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

要查看 apt-get 可以和不可以执行的操作列表,请man apt-get在终端中输入 并点击Enter。您也可以使用任何其他命令执行此操作。例如,man sudoman nano,甚至man chromium-browser

答案2

您可以简单地在同一个命令中列出要安装的多个包。我相信这也会只给您一个 y/n 提示。示例:

sudo apt-get install docky geany firefox

答案3

我过去发现,如果其中一个命令出现“故障”,以这种方式将命令串在一起就会导致问题。

例如,如果您的命令类似于此:

sudo apt-get install 1 2 3 4

然后,如果程序 3 出现错误,则不会安装 4。将它们分开放在每行上可以解决这个问题。如果出现错误,脚本将继续运行。

这是我用于新安装的脚本示例:

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

相关内容