单文件 bash 脚本中的菜单及其子菜单,每个菜单都有一个返回根菜单。
我的问题是,调用show_root_menu
有时只是跳转到上层菜单而不是根目录。
回到根菜单时,nvidia 子菜单最常出错。
show_pckg_menu
相反,当从“安装图形专有驱动程序”子菜单中选择“显示根菜单”时,它会转到“包选项” :
到目前为止我的其他相关问题已解决:
$ shellcheck myscript No issues detected!
实际的脚本是:
#!/bin/bash
show_root_menu(){
width=72
height=50
menu_height=8
backtitle="Installer-menu"
title="Setup opions"
menu="Choose one of the following options:"
options=(1 'Add Mint PPA and update'
2 'Install Cinnamon '
3 'update and upgrade '
4 'Additional software installation '
5 'Upgrade Kernel '
6 'Resolve Ubuntu Cinnamon issues '
7 'Install graphic proprietary driver'
x reboot
q quit )
result=$(dialog --clear \
--backtitle "$backtitle" \
--title "$title" \
--menu "$menu" \
$height $width $menu_height \
"${options[@]}" \
2>&1 >/dev/tty)
}
show_pckg_menu(){
width=72
height=22
menu_height=8
backtitle='Installer-menu'
title='Package options'
menu='Choose one of the following options:'
options=(1 'Install package list'
2 'Export package list'
3 'update and upgrade'
4 'Show root menu'
x reboot
q quit )
result=$(dialog --clear \
--backtitle "$backtitle" \
--title "$title" \
--menu "$menu" \
$height $width $menu_height \
"${options[@]}" \
2>&1 >/dev/tty)
}
show_solver_menu(){
width=72
height=22
menu_height=8
backtitle='Installer-menu'
title='Resolver menu'
menu='Choose one of the following options:'
options=(1 'Network applet problem'
2 'Sound problem'
3 'update and upgrade'
4 'Show root menu'
x reboot
q quit )
result=$(dialog --clear \
--backtitle "$backtitle" \
--title "$title" \
--menu "$menu" \
$height $width $menu_height \
"${options[@]}" \
2>&1 >/dev/tty)
}
show_nvidia_menu(){
width=72
height=22
menu_height=8
backtitle='Installer-menu'
title='NVidia driver selection'
menu='Choose one of the following options:'
options=(1 'Install nvidia 515'
2 'Install nvidia 525'
3 'Update and upgrade'
4 'Show root menu'
x reboot
q quit )
result=$(dialog --clear \
--backtitle "$backtitle" \
--title "$title" \
--menu "$menu" \
$height $width $menu_height \
"${options[@]}" \
2>&1 >/dev/tty)
}
show_root_menu
case "$result" in
1) echo 'Mint backport repos installed';
apt-key adv --recv-keys --keyserver keyserver.ubuntu.com A1715D88E1DF1F24 40976EAF437D05B5 3B4FE6ACC0B21F32 A6616109451BBBF2;
sh -c 'echo "deb http://packages.linuxmint.com vanessa main upstream import backport romeo" >> /etc/apt/sources.list.d/mint.list';
sh -c 'echo "deb-src http://packages.linuxmint.com vanessa main upstream import backport romeo" >> /etc/apt/sources.list.d/mint.list';
apt-key export 451BBBF2 | gpg --dearmour -o /etc/apt/trusted.gpg.d/mint.gpg;
apt update;
show_root_menu;;
2) echo 'Installation of Cinnamon';
apt install slick-greeter muffin cinnamon;
show_root_menu;;
3) echo 'Package upgrade';
apt update && apt upgrade -y;
show_root_menu;;
4) show_pckg_menu;;
5) sh ubuntu-mainline-kernel.sh;
show_root_menu;;
6) show_solver_menu;;
7) show_nvidia_menu;;
x) echo Reboot;
reboot;;
q) clear;
exit ;;
esac
show_pckg_menu
case "$result" in
1) echo 'Package Install';
sh installpkgs.sh;;
2) echo 'Manualy installed packages exported';
sh pkgsexport.sh;;
3) echo 'Package upgrade';
apt update && apt upgrade -y;
show_pckg_menu;;
4) show_root_menu;;
x) echo 'Reboot';
reboot;;
q) clear;
exit ;;
esac
show_solver_menu
case "$result" in
1) echo 'Applying changes to network settings';
clear;
option_picked "Option 1 Picked";
touch /etc/network/interfaces;
sh -c 'echo "auto lo" >> /etc/network/interfaces';
sh -c 'echo "iface lo inet loopback" >> /etc/network/interfaces';
sh -c 'echo " renderer: NetworkManager" >> /etc/netplan/*.yaml';
netplan apply;
show_solver_menu;;
2) echo 'Applying changes to sound settings';
ln -s /usr/share/pipewire /etc/pipewire;
touch /usr/share/pipewire/media-session.d/with-pulseaudio;
systemctl --user restart pipewire-session-manager;
systemctl --user start pulseaudio;
show_solver_menu;;
3) echo 'Package upgrade';
apt update && apt upgrade -y;
show_solver_menu;;
4) show_root_menu;;
x) echo 'Reboot';
reboot;;
q) clear;
exit ;;
esac
show_nvidia_menu
case "$result" in
1) echo 'NVidia driver 515 installation';
apt install nvidia-driver-515 -y;
show_nvidia_menu;;
2) echo 'NVidia driver 525 installation';
apt install nvidia-driver-525 -y;
show_nvidia_menu;;
3) echo 'Package upgrade';
apt update && apt upgrade -y;
show_nvidia_menu;;
4) show_root_menu;;
x) echo 'Reboot';
reboot;;
q) clear; exit ;;
esac
答案1
在安装程序的末尾,有四个菜单调用show_root_menu
、show_pckg_menu
和,每个菜单show_solver_menu
调用show_nvidia_menu
都遵循相应的 case 语句。
发生的情况是这样的:当显示根菜单并且用户按下 时cancel
,将显示包菜单。按cancel
-> 解算器菜单,cancel
-> nvidia 菜单,cancel
-> 退出(脚本结束)。
如果再次启动脚本并按7
(nvidia 菜单),然后按4
返回根菜单,则 case 语句将丢失并被show_pckg_menu
调用。
修复:仅show_root_menu
在脚本末尾调用,并将 case 语句移至 后相应的函数中result=$(dialog...)
。
#!/bin/bash
show_root_menu(){
width=72
height=50
menu_height=8
backtitle="Installer-menu"
title="Setup opions"
menu="Choose one of the following options:"
options=(1 'Add Mint PPA and update'
2 'Install Cinnamon '
3 'update and upgrade '
4 'Additional software installation '
5 'Upgrade Kernel '
6 'Resolve Ubuntu Cinnamon issues '
7 'Install graphic proprietary driver'
x reboot
q quit )
result=$(dialog --clear \
--backtitle "$backtitle" \
--title "$title" \
--menu "$menu" \
$height $width $menu_height \
"${options[@]}" \
2>&1 >/dev/tty)
case "$result" in
1) echo 'Mint backport repos installed';
apt-key adv --recv-keys --keyserver keyserver.ubuntu.com A1715D88E1DF1F24 40976EAF437D05B5 3B4FE6ACC0B21F32 A6616109451BBBF2;
sh -c 'echo "deb http://packages.linuxmint.com vanessa main upstream import backport romeo" >> /etc/apt/sources.list.d/mint.list';
sh -c 'echo "deb-src http://packages.linuxmint.com vanessa main upstream import backport romeo" >> /etc/apt/sources.list.d/mint.list';
apt-key export 451BBBF2 | gpg --dearmour -o /etc/apt/trusted.gpg.d/mint.gpg;
apt update;
show_root_menu;;
2) echo 'Installation of Cinnamon';
apt install slick-greeter muffin cinnamon;
show_root_menu;;
3) echo 'Package upgrade';
apt update && apt upgrade -y;
show_root_menu;;
4) show_pckg_menu;;
5) sh ubuntu-mainline-kernel.sh;
show_root_menu;;
6) show_solver_menu;;
7) show_nvidia_menu;;
x) echo Reboot;
reboot;;
q) clear;
exit ;;
esac
}
show_pckg_menu(){
width=72
height=22
menu_height=8
backtitle='Installer-menu'
title='Package options'
menu='Choose one of the following options:'
options=(1 'Install package list'
2 'Export package list'
3 'update and upgrade'
4 'Show root menu'
x reboot
q quit )
result=$(dialog --clear \
--backtitle "$backtitle" \
--title "$title" \
--menu "$menu" \
$height $width $menu_height \
"${options[@]}" \
2>&1 >/dev/tty)
case "$result" in
1) echo 'Package Install';
sh installpkgs.sh;;
2) echo 'Manualy installed packages exported';
sh pkgsexport.sh;;
3) echo 'Package upgrade';
apt update && apt upgrade -y;
show_pckg_menu;;
4) show_root_menu;;
x) echo 'Reboot';
reboot;;
q) clear;
exit ;;
esac
}
show_solver_menu(){
width=72
height=22
menu_height=8
backtitle='Installer-menu'
title='Resolver menu'
menu='Choose one of the following options:'
options=(1 'Network applet problem'
2 'Sound problem'
3 'update and upgrade'
4 'Show root menu'
x reboot
q quit )
result=$(dialog --clear \
--backtitle "$backtitle" \
--title "$title" \
--menu "$menu" \
$height $width $menu_height \
"${options[@]}" \
2>&1 >/dev/tty)
case "$result" in
1) echo 'Applying changes to network settings';
clear;
option_picked "Option 1 Picked";
touch /etc/network/interfaces;
sh -c 'echo "auto lo" >> /etc/network/interfaces';
sh -c 'echo "iface lo inet loopback" >> /etc/network/interfaces';
sh -c 'echo " renderer: NetworkManager" >> /etc/netplan/*.yaml';
netplan apply;
show_solver_menu;;
2) echo 'Applying changes to sound settings';
ln -s /usr/share/pipewire /etc/pipewire;
touch /usr/share/pipewire/media-session.d/with-pulseaudio;
systemctl --user restart pipewire-session-manager;
systemctl --user start pulseaudio;
show_solver_menu;;
3) echo 'Package upgrade';
apt update && apt upgrade -y;
show_solver_menu;;
4) show_root_menu;;
x) echo 'Reboot';
reboot;;
q) clear;
exit ;;
esac
}
show_nvidia_menu(){
width=72
height=22
menu_height=8
backtitle='Installer-menu'
title='NVidia driver selection'
menu='Choose one of the following options:'
options=(1 'Install nvidia 515'
2 'Install nvidia 525'
3 'Update and upgrade'
4 'Show root menu'
x reboot
q quit )
result=$(dialog --clear \
--backtitle "$backtitle" \
--title "$title" \
--menu "$menu" \
$height $width $menu_height \
"${options[@]}" \
2>&1 >/dev/tty)
case "$result" in
1) echo 'NVidia driver 515 installation';
apt install nvidia-driver-515 -y;
show_nvidia_menu;;
2) echo 'NVidia driver 525 installation';
apt install nvidia-driver-525 -y;
show_nvidia_menu;;
3) echo 'Package upgrade';
apt update && apt upgrade -y;
show_nvidia_menu;;
4) show_root_menu;;
x) echo 'Reboot';
reboot;;
q) clear; exit ;;
esac
}
show_root_menu