gpt4 book ai didi

linux - 返回主菜单后 Bash 脚本不执行更多命令

转载 作者:太空宇宙 更新时间:2023-11-04 12:28:03 24 4
gpt4 key购买 nike

我已经为发行版 deepin 创建了一个带有一些安装后命令的 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

最佳答案

我建议重构您的代码,使循环位于外部:

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

关于linux - 返回主菜单后 Bash 脚本不执行更多命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44075166/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com