创建和调试 bash 脚本菜单

创建和调试 bash 脚本菜单

我第一次尝试使用 sh 就像我在其他帖子中提到的那样这里

我正在尝试运行 bash 脚本菜单以仅从终端运行。

#!/bin/bash

HEIGHT=800
WIDTH=600
CHOICE_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 )
    
RESULT=$(dialog --clear \
                --backtitle "$BACKTITLE" \
                --title "$TITLE" \
                --menu "$MENU" \
                $HEIGHT $WIDTH $CHOICE_HEIGHT \
                "${OPTIONS[@]}" \
                2>&1 >/dev/tty)

case $RESULT in
    1) sudo apt-key adv --recv-keys --keyserver keyserver.ubuntu.com A1715D88E1DF1F24 40976EAF437D05B5 3B4FE6ACC0B21F32 A6616109451BBBF2;
       sudo sh -c 'echo "deb http://packages.linuxmint.com vanessa main upstream import backport romeo" >> /etc/apt/sources.list.d/mint.list';
       sudo sh -c 'echo "deb-src http://packages.linuxmint.com vanessa main upstream import backport romeo" >> /etc/apt/sources.list.d/mint.list';
       sudo apt-key export 451BBBF2 | gpg --dearmour -o /etc/apt/trusted.gpg.d/mint.gpg;
       sudo apt update;;
    2) sudo apt install slick-greeter muffin cinnamon;;
    3) sudo apt update;
       sudo apt upgrade -y;;
    4) sudo sh additional-software.sh;;
    5) sudo sh ubuntu-mainline-kernel.sh;;
    6) sudo sh problem-solver.sh;;
    7) sudo sh nvidia-installation;;
    *) reboot;;
esac

和一个较小的:

#!/bin/bash

HEIGHT=800
WIDTH=600
CHOICE_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 
          x reboot )
    
RESULT=$(dialog --clear \
                --backtitle "$BACKTITLE" \
                --title "$TITLE" \
                --menu "$MENU" \
                $HEIGHT $WIDTH $CHOICE_HEIGHT \
                "${OPTIONS[@]}" \
                2>&1 >/dev/tty)

case $RESULT in
     1) while IFS= read -r line
do
  echo "apt install -y $line"
done < installation.txt;;
     2) awk -F'll ' '
  /apt install/ && !/nvidia/ && !/--/ && !/-f/{ print $2 }
' /var/log/apt/history.log >installation.txt;;
     3) sudo apt update && sudo apt upgrade;;
     *) reboot;;
esac

$ shellcheck myscript
No issues detected!

我想我在两者上都忽略了同样的事情。两个文件都只运行最后一个命令。

有人可以启发我吗?

使用参考

我得到了能够运行软件包的每个人的帮助(=bash 脚本文件名)菜单内含:

#!/bin/bash

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'
         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;;
     x) echo Reboot;
        reboot;;
     q) clear; exit ;;
esac

这两个 sh 文件分别包含:

#!/bin/sh
# Export manualy installed packages 
# Packages installed with apt install from terminal excl.
# Output file: installation.txt

awk -F'll ' '  /apt install/ && !/nvidia/ && !/--/ && !/-f/{ print $2 }  ' /var/log/apt/history.log >installation.txt

#!/bin/sh
# Install package list
for pkg in `cat installation.txt`; do sudo apt-get install -y $pkg; done

生成所有列出选项的工作菜单: 包 bash 脚本


对于安装程序菜单,我尝试这样应用它:

#!/bin/bash

width=72
height=22
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;;
    2)  echo 'Installation of Cinnamon';
        apt install slick-greeter muffin cinnamon;;
    3)  echo 'Package upgrade';
        apt update && apt upgrade -y;;
    4)  sh additional-software.sh;;
    5)  sh ubuntu-mainline-kernel.sh;;
    6)  sh problem-solver.sh;;
    7)  sh nvidia-installation;;
     x) echo Reboot;
        reboot;;
     q) clear; exit ;;
esac

菜单是这样显示的:

安装程序菜单

但我忘记输入之间的每个标题' '

答案1

这是我在评论中谈论的内容的(空运行,仅回显)示例。

  1. 使用options=()而不是options=$()创建数组。数组列表和命令替换不是同一件事。
  2. 数组的多字元素需要用引号引起来。
  3. 小写变量名。

还:

  1. 高度和宽度dialog以字符而非像素指定,因此我使用 72x22 而不是 800x600。这足以填满“标准”80x25 屏幕或终端的大部分区域。或者,只需将它们都设置为0即可dialog使菜单达到所需的大小。
  2. 我还将您的变量重命名CHOICE_HEIGHTmenu_height,主要是因为这就是它在dialog手册页中的记录方式。
  3. 双引号适用于需要将变量等插入字符串时。单引号更适合固定字符串。
  4. 我添加了一个“退出”选项(除了 提供的取消按钮之外dialog --menu)并更改了case语句,以便重新启动不是默认选项。这是一个糟糕的默认设置,如果用户犯了错误或使用了“取消”按钮,您不希望计算机重新启动。

上面最重要的项目(1 和 2)已经在您开始使用的 askubuntu.com 上的参考示例中,宽度和高度均为 40x15。

#!/bin/bash

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'
         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 while IFS= read -r line ... ;;
     2) echo awk -F'll' ... ;;
     3) echo 'sudo apt update && sudo apt upgrade' ;;
     x) echo reboot ;;
     q) clear; exit ;;
esac

相关内容