Bash脚本在返回主菜单

时间:2017-05-19 16:46:49

标签: linux bash shell

我创建了一个bash脚本,其中包含一些用于发行版深度的安装后命令。现在我的问题是,我真的不知道如何解决因为我是bash脚本的新手,是在选择并执行命令之后,有一个命令返回到主菜单的脚本。但是当我在主菜单上时,脚本无法执行另一个命令。如果我选择一个选项并尝试执行,则无需任何操作。 最小接口的脚本使用对话框。 有人可以帮我理解是什么问题吗?

这是代码

 #!/bin/bash 
if [[ $EUID -ne 0 ]]; then
 echo "Questo script deve essere eseguito come root" 
 exit 1
else

sudo apt-get install dialog
 cmd=(dialog --separate-output --checklist "Seleziona i programmi che vuoi installare:" 22 76 16)
 options=(1 "Impostazione Mirror GARR" off # qualsiasi impostazione può essere impostata su "on"
 2 "Aggiornamento di sistema" off
 3 "Installazione font Microsoft" off
 4 "Installazione Gdebi" off
 5 "Synaptic" off
 6 "BleachBit" off
 7 "Open JDK 8" off
 8 "Supporto lettura DVD" off
 9 "LibreOffice" off
 10 "VLC Media Player" off
 11 "Flash Player" off
 12 "Google Chrome" off
 13 "Teamiewer" off
 14 "Skype" off
 15 "Brasero" off
 16 "iFuse per supporto device Apple" off
 17 "Kodi" off
 18 "Gimp" off
 19 "Telegram" off 
 20 "Enpass Password manager" off
 21 "Opera Browser" off
 22 "GUFW" off
 23 "Vivaldi Browser" off
 24 "Risparmio energetico TLP" off
 25 "Pulizia del sistema" off
 26 "Esci" off)
 choices=$("${cmd[@]}" "${options[@]}" 2>&1 >/dev/tty)

 for choice in $choices
 do
 case $choice in

 1)
 #Setting mirror on italian server GARR
  echo "Impostazione Mirror GARR"
 cd $HOME || exit
cp /etc/apt/sources.list backup.sources.list
bash -c 'cat << EOF > /etc/apt/sources.list
# Generated by deepin-installer
# deb [by-hash=force] http://packages.deepin.com/deepin unstable main contrib non-free
# deb-src http://packages.deepin.com/deepin unstable main contrib non-free
##########################################################################################
deb [by-hash=force] http://ba.mirror.garr.it/mirrors/deepin/ panda main contrib non-free
EOF'
      sudo apt-get update
 ;;

2)
 #Update of the repo and upgrade the system

  echo "Aggiornamento del sistema"
apt-get update && sudo apt-get upgrade -y
  echo "Operazione completata. Torno al menu principale"
sleep 2
while [ "$choices" -ne "26" ];do choices=$("${cmd[@]}" "${options[@]}" 2>&1 >/dev/tty);case $choices in 1) echo "number one" && sleep 5;;esac;done
;;
 3) 
 #Install Microsoft Fonts

  echo "Installazione Font Microsoft"
apt-get install ttf-mscorefonts-installer -y
  echo "Operazione completata. Torno al menu principale"
sleep 2
while [ "$choices" -ne "26" ];do choices=$("${cmd[@]}" "${options[@]}" 2>&1 >/dev/tty);case $choices in 1) echo "number one" && sleep 5;;esac;done
 ;;

 4)
 #Install Gdebi

  echo "Installazione Gdebi"
apt-get install gdebi -y
  echo "Operazione completata. Torno al menu principale"
sleep 2
while [ "$choices" -ne "26" ];do choices=$("${cmd[@]}" "${options[@]}" 2>&1 >/dev/tty);case $choices in 1) echo "number one" && sleep 5;;esac;done
 ;;

5)
 #Install Synaptic

  echo "Installazione Synaptic"
apt-get install synaptic -y
  echo "Operazione completata. Torno al menu principale"
sleep 2
while [ "$choices" -ne "26" ];do choices=$("${cmd[@]}" "${options[@]}" 2>&1 >/dev/tty);case $choices in 1) echo "number one" && sleep 5;;esac;done
 ;;

 6)
 #Install BleachBit

  echo "Installazione Bleachbit"
apt-get install bleachbit -y
  echo "Operazione completata. Torno al menu principale"
sleep 2
while [ "$choices" -ne "26" ];do choices=$("${cmd[@]}" "${options[@]}" 2>&1 >/dev/tty);case $choices in 1) echo "number one" && sleep 5;;esac;done
 ;;

 7)
 #Install JDK 8

  echo "Installazione Open JDK8"
apt-get install openjdk-8-jre icedtea-8-plugin -y
  echo "Operazione completata. Torno al menu principale"
sleep 2
while [ "$choices" -ne "26" ];do choices=$("${cmd[@]}" "${options[@]}" 2>&1 >/dev/tty);case $choices in 1) echo "number one" && sleep 5;;esac;done
 ;;

 8)
 #Install support for commercial DVD

  echo "Installazione supporto DVD"
apt-get install libdvd-pkg libdvdread4 python3 -y && dpkg-reconfigure libdvd-pkg;
  echo "Operazione completata. Torno al menu principale"
sleep 2
while [ "$choices" -ne "26" ];do choices=$("${cmd[@]}" "${options[@]}" 2>&1 >/dev/tty);case $choices in 1) echo "number one" && sleep 5;;esac;done
 ;;

 9)
 #Install the complete suite of LibreOffice and the italian help & translation

  echo "Installazione LibreOffice"
apt-get install libreoffice libreoffice-help-it libreoffice-l10n-it -y
  echo "Operazione completata. Torno al menu principale"
sleep 2
while [ "$choices" -ne "26" ];do choices=$("${cmd[@]}" "${options[@]}" 2>&1 >/dev/tty);case $choices in 1) echo "number one" && sleep 5;;esac;done
 ;;

 10)
 #Install VLC Media Player

  echo "Installazione VLC Media Player"
apt-get install vlc -y
  echo "Operazione completata. Torno al menu principale"
sleep 2
while [ "$choices" -ne "26" ];do choices=$("${cmd[@]}" "${options[@]}" 2>&1 >/dev/tty);case $choices in 1) echo "number one" && sleep 5;;esac;done
 ;;

 11)
 #Install Flash Player

  echo "Installazione Flash Player"
apt-get install flashplugin-nonfree -y
  echo "Operazione completata. Torno al menu principale"
sleep 2
while [ "$choices" -ne "26" ];do choices=$("${cmd[@]}" "${options[@]}" 2>&1 >/dev/tty);case $choices in 1) echo "number one" && sleep 5;;esac;done
 ;;

 12)
#Install Chrome browser

  echo "Installazione Google Chrome"
wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | sudo apt-key add -
sh -c 'echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google-chrome.list'
apt-get update 
apt-get purge google-chrome-stable -y
apt-get install google-chrome-stable -y
  echo "Operazione completata. Torno al menu principale"
sleep 2
while [ "$choices" -ne "26" ];do choices=$("${cmd[@]}" "${options[@]}" 2>&1 >/dev/tty);case $choices in 1) echo "number one" && sleep 5;;esac;done
 ;;

 13)
 #Install Teamviewer

  echo "Installazione Teamviewer"
wget http://download.teamviewer.com/download/teamviewer_i386.deb
dpkg -i teamviewer_i386.deb
apt-get install -f -y
rm -rf teamviewer_i386.deb
  echo "Operazione completata. Torno al menu principale"
sleep 2
while [ "$choices" -ne "26" ];do choices=$("${cmd[@]}" "${options[@]}" 2>&1 >/dev/tty);case $choices in 1) echo "number one" && sleep 5;;esac;done
 ;;

 14)
#Install Skype for Linux

  echo "Installing Skype For Linux"
 apt install apt-transport-https -y
 curl https://repo.skype.com/data/SKYPE-GPG-KEY | apt-key add -
 echo "deb https://repo.skype.com/deb stable main" | tee /etc/apt/sources.list.d/skypeforlinux.list
 apt-get update 
 apt-get install skypeforlinux -y
   echo "Operazione completata. Torno al menu principale"
sleep 2
 while [ "$choices" -ne "26" ];do choices=$("${cmd[@]}" "${options[@]}" 2>&1 >/dev/tty);case $choices in 1) echo "number one" && sleep 5;;esac;done
 ;;

 15)
#Install Brasero

  echo "Installazione Brasero"
apt-get install brasero -y
  echo "Operazione completata. Torno al menu principale"
sleep 2
while [ "$choices" -ne "26" ];do choices=$("${cmd[@]}" "${options[@]}" 2>&1 >/dev/tty);case $choices in 1) echo "number one" && sleep 5;;esac;done
 ;;

 16)
 #Install iFuse for support iDevice

  echo "Installazione supporto per device Apple"
apt-get install ifuse -y
  echo "Operazione completata. Torno al menu principale"
sleep 2
while [ "$choices" -ne "26" ];do choices=$("${cmd[@]}" "${options[@]}" 2>&1 >/dev/tty);case $choices in 1) echo "number one" && sleep 5;;esac;done
 ;;

 17)
#Install Kodi

  echo "Installazione Kodi"
apt-get install kodi -y
  echo "Operazione completata. Torno al menu principale"
sleep 2
while [ "$choices" -ne "26" ];do choices=$("${cmd[@]}" "${options[@]}" 2>&1 >/dev/tty);case $choices in 1) echo "number one" && sleep 5;;esac;done
 ;;

 18)
 #Install Gimp

  echo "Installazione Gimp"
apt-get install gimp gimp-plugin-registry -y
  echo "Operazione completata. Torno al menu principale"
sleep 2
while [ "$choices" -ne "26" ];do choices=$("${cmd[@]}" "${options[@]}" 2>&1 >/dev/tty);case $choices in 1) echo "number one" && sleep 5;;esac;done
 ;;

19)
 #Install Telegram

 echo "Installazione Telegram"
apt-get install telegram -y
  echo "Operazione completata. Torno al menu principale"
sleep 2
while [ "$choices" -ne "26" ];do choices=$("${cmd[@]}" "${options[@]}" 2>&1 >/dev/tty);case $choices in 1) echo "number one" && sleep 5;;esac;done
 ;;

20)
 #Install Enpass Password manager

 echo "Installazione Enpass Password manager"
 echo "deb http://repo.sinew.in/ stable main" | tee /etc/apt/sources.list.d/enpass.list
curl https://dl.sinew.in/keys/enpass-linux.key | apt-key add -
apt-get update
apt-get install enpass -y
  echo "Operazione completata. Torno al menu principale"
sleep 2
while [ "$choices" -ne "26" ];do choices=$("${cmd[@]}" "${options[@]}" 2>&1 >/dev/tty);case $choices in 1) echo "number one" && sleep 5;;esac;done
 ;;

 21)
 #Install Opera Browser

  echo "Installazione Opera Browser"
sudo add-apt-repository 'deb https://deb.opera.com/opera-stable/ stable non-free'
wget -qO- https://deb.opera.com/archive.key | sudo apt-key add -
apt-get update && apt-get install opera-stable -y
  echo "Operazione completata. Torno al menu principale"
sleep 2
while [ "$choices" -ne "26" ];do choices=$("${cmd[@]}" "${options[@]}" 2>&1 >/dev/tty);case $choices in 1) echo "number one" && sleep 5;;esac;done
 ;;

 22)
 #Install GUFW

  echo "Installazione GUFW"
apt-get install gufw -y
  echo "Operazione completata. Torno al menu principale"
sleep 2
while [ "$choices" -ne "26" ];do choices=$("${cmd[@]}" "${options[@]}" 2>&1 >/dev/tty);case $choices in 1) echo "number one" && sleep 5;;esac;done
 ;;

23)
#Install Vivaldi Browser

 echo "Installazione Vivaldi Browser"
curl http://repo.vivaldi.com/stable/linux_signing_key.pub | apt-key add -
echo "deb http://repo.vivaldi.com/stable/deb/ stable main" | tee /etc/apt/sources.list.d/vivaldibrowser.list
apt-get update
apt-get install vivaldi-stable -y
  echo "Operazione completata. Torno al menu principale"
sleep 2
while [ "$choices" -ne "26" ];do choices=$("${cmd[@]}" "${options[@]}" 2>&1 >/dev/tty);case $choices in 1) echo "number one" && sleep 5;;esac;done
;;

24)
#Install TLP
 echo "Installazione Risparmio energetico TLP"
apt-get install tlp tlp-rdw && tlp start
  echo "Operazione completata. Torno al menu principale"
sleep 2
while [ "$choices" -ne "26" ];do choices=$("${cmd[@]}" "${options[@]}" 2>&1 >/dev/tty);case $choices in 1) echo "number one" && sleep 5;;esac;done
;;

25)
#Clean the system from packages no more useful
 echo "Pulizia del sistema"
apt-get --purge autoremove
apt-get autoclean
apt-get clean
rm -fr /tmp/*
rm -rfv ~/.local/share/Trash/*
apt-get update
 echo "Operazione completata. Torno al menu principale"
sleep 2
while [ "$choices" -ne "26" ];do choices=$("${cmd[@]}" "${options[@]}" 2>&1 >/dev/tty);case $choices in 1) echo "number one" && sleep 5;;esac;done
;;

26)
#Exit from script
exit 0
  esac
 done
fi

1 个答案:

答案 0 :(得分:0)

我建议重构你的代码,以便循环在外面:

while true; do
    echo "Would you like to:"
    echo "1 - Print hello world"
    echo "26 - Exit"
    read -p "Enter your choice: " VAL;
    case $VAL in 
    1) 
        echo "Hello world!"
    ;;
    26)
        echo "Going away now."
        break
    ;;
    esac
done

这使得更清楚的是发生了什么。

这会使整个脚本看起来像这样:

#!/bin/bash 
if [[ $EUID -ne 0 ]]; then
 echo "Questo script deve essere eseguito come root" 
 exit 1
fi

sudo apt-get install dialog
 cmd=(dialog --separate-output --checklist "Seleziona i programmi che vuoi installare:" 22 76 16)
 options=(1 "Impostazione Mirror GARR" off # qualsiasi impostazione può essere impostata su "on"
 2 "Aggiornamento di sistema" off
 3 "Installazione font Microsoft" off
 4 "Installazione Gdebi" off
 5 "Synaptic" off
 6 "BleachBit" off
 7 "Open JDK 8" off
 8 "Supporto lettura DVD" off
 9 "LibreOffice" off
 10 "VLC Media Player" off
 11 "Flash Player" off
 12 "Google Chrome" off
 13 "Teamiewer" off
 14 "Skype" off
 15 "Brasero" off
 16 "iFuse per supporto device Apple" off
 17 "Kodi" off
 18 "Gimp" off
 19 "Telegram" off 
 20 "Enpass Password manager" off
 21 "Opera Browser" off
 22 "GUFW" off
 23 "Vivaldi Browser" off
 24 "Risparmio energetico TLP" off
 25 "Pulizia del sistema" off
 26 "Esci" off)

 while true; do
 choices=$("${cmd[@]}" "${options[@]}" 2>&1 >/dev/tty)

 for choice in $choices
 do
 case $choice in

 1)
 #Setting mirror on italian server GARR
  echo "Impostazione Mirror GARR"
 cd $HOME || exit
cp /etc/apt/sources.list backup.sources.list
bash -c 'cat << EOF > /etc/apt/sources.list
# Generated by deepin-installer
# deb [by-hash=force] http://packages.deepin.com/deepin unstable main contrib non-free
# deb-src http://packages.deepin.com/deepin unstable main contrib non-free
##########################################################################################
deb [by-hash=force] http://ba.mirror.garr.it/mirrors/deepin/ panda main contrib non-free
EOF'
      sudo apt-get update
 ;;

2)
 #Update of the repo and upgrade the system

  echo "Aggiornamento del sistema"
apt-get update && sudo apt-get upgrade -y
  echo "Operazione completata. Torno al menu principale"
sleep 2
;;
 3) 
 #Install Microsoft Fonts

  echo "Installazione Font Microsoft"
apt-get install ttf-mscorefonts-installer -y
  echo "Operazione completata. Torno al menu principale"
sleep 2
 ;;

 4)
 #Install Gdebi

  echo "Installazione Gdebi"
apt-get install gdebi -y
  echo "Operazione completata. Torno al menu principale"
sleep 2
 ;;

5)
 #Install Synaptic

  echo "Installazione Synaptic"
apt-get install synaptic -y
  echo "Operazione completata. Torno al menu principale"
sleep 2
 ;;

 6)
 #Install BleachBit

  echo "Installazione Bleachbit"
apt-get install bleachbit -y
  echo "Operazione completata. Torno al menu principale"
sleep 2
 ;;

 7)
 #Install JDK 8

  echo "Installazione Open JDK8"
apt-get install openjdk-8-jre icedtea-8-plugin -y
  echo "Operazione completata. Torno al menu principale"
sleep 2
 ;;

 8)
 #Install support for commercial DVD

  echo "Installazione supporto DVD"
apt-get install libdvd-pkg libdvdread4 python3 -y && dpkg-reconfigure libdvd-pkg;
  echo "Operazione completata. Torno al menu principale"
sleep 2
 ;;

 9)
 #Install the complete suite of LibreOffice and the italian help & translation

  echo "Installazione LibreOffice"
apt-get install libreoffice libreoffice-help-it libreoffice-l10n-it -y
  echo "Operazione completata. Torno al menu principale"
sleep 2
 ;;

 10)
 #Install VLC Media Player

  echo "Installazione VLC Media Player"
apt-get install vlc -y
  echo "Operazione completata. Torno al menu principale"
sleep 2
 ;;

 11)
 #Install Flash Player

  echo "Installazione Flash Player"
apt-get install flashplugin-nonfree -y
  echo "Operazione completata. Torno al menu principale"
sleep 2
 ;;

 12)
#Install Chrome browser

  echo "Installazione Google Chrome"
wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | sudo apt-key add -
sh -c 'echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google-chrome.list'
apt-get update 
apt-get purge google-chrome-stable -y
apt-get install google-chrome-stable -y
  echo "Operazione completata. Torno al menu principale"
sleep 2
 ;;

 13)
 #Install Teamviewer

  echo "Installazione Teamviewer"
wget http://download.teamviewer.com/download/teamviewer_i386.deb
dpkg -i teamviewer_i386.deb
apt-get install -f -y
rm -rf teamviewer_i386.deb
  echo "Operazione completata. Torno al menu principale"
sleep 2
 ;;

 14)
#Install Skype for Linux

  echo "Installing Skype For Linux"
 apt install apt-transport-https -y
 curl https://repo.skype.com/data/SKYPE-GPG-KEY | apt-key add -
 echo "deb https://repo.skype.com/deb stable main" | tee /etc/apt/sources.list.d/skypeforlinux.list
 apt-get update 
 apt-get install skypeforlinux -y
   echo "Operazione completata. Torno al menu principale"
sleep 2
  ;;

 15)
#Install Brasero

  echo "Installazione Brasero"
apt-get install brasero -y
  echo "Operazione completata. Torno al menu principale"
sleep 2
 ;;

 16)
 #Install iFuse for support iDevice

  echo "Installazione supporto per device Apple"
apt-get install ifuse -y
  echo "Operazione completata. Torno al menu principale"
sleep 2
 ;;

 17)
#Install Kodi

  echo "Installazione Kodi"
apt-get install kodi -y
  echo "Operazione completata. Torno al menu principale"
sleep 2
 ;;

 18)
 #Install Gimp

  echo "Installazione Gimp"
apt-get install gimp gimp-plugin-registry -y
  echo "Operazione completata. Torno al menu principale"
sleep 2
 ;;

19)
 #Install Telegram

 echo "Installazione Telegram"
apt-get install telegram -y
  echo "Operazione completata. Torno al menu principale"
sleep 2
 ;;

20)
 #Install Enpass Password manager

 echo "Installazione Enpass Password manager"
 echo "deb http://repo.sinew.in/ stable main" | tee /etc/apt/sources.list.d/enpass.list
curl https://dl.sinew.in/keys/enpass-linux.key | apt-key add -
apt-get update
apt-get install enpass -y
  echo "Operazione completata. Torno al menu principale"
sleep 2
 ;;

 21)
 #Install Opera Browser

  echo "Installazione Opera Browser"
sudo add-apt-repository 'deb https://deb.opera.com/opera-stable/ stable non-free'
wget -qO- https://deb.opera.com/archive.key | sudo apt-key add -
apt-get update && apt-get install opera-stable -y
  echo "Operazione completata. Torno al menu principale"
sleep 2
 ;;

 22)
 #Install GUFW

  echo "Installazione GUFW"
apt-get install gufw -y
  echo "Operazione completata. Torno al menu principale"
sleep 2
 ;;

23)
#Install Vivaldi Browser

 echo "Installazione Vivaldi Browser"
curl http://repo.vivaldi.com/stable/linux_signing_key.pub | apt-key add -
echo "deb http://repo.vivaldi.com/stable/deb/ stable main" | tee /etc/apt/sources.list.d/vivaldibrowser.list
apt-get update
apt-get install vivaldi-stable -y
  echo "Operazione completata. Torno al menu principale"
sleep 2
;;

24)
#Install TLP
 echo "Installazione Risparmio energetico TLP"
apt-get install tlp tlp-rdw && tlp start
  echo "Operazione completata. Torno al menu principale"
sleep 2
;;

25)
#Clean the system from packages no more useful
 echo "Pulizia del sistema"
apt-get --purge autoremove
apt-get autoclean
apt-get clean
rm -fr /tmp/*
rm -rfv ~/.local/share/Trash/*
apt-get update
 echo "Operazione completata. Torno al menu principale"
sleep 2
;;

26)
#Exit from script
exit 0
  esac
 done # For Loop
done # While loop