2017-05-19 101 views
0

我已經爲發行版deepin創建了一些bash腳本,其中包含一些後期安裝命令。現在我的問題,我真的不知道如何解決,因爲我是一個非常新手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 
+0

爲什麼是'退出0'你'case'語句中? –

+0

對不起,這是一個錯字。 – Fasbyte01

回答

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 
+0

謝謝@coder_man。你的解決方案解決了我的問題 – Fasbyte01