0

I've tried a first time with sh like I refer to in my other post here.

I'm trying to run a bash script menu to run from terminal only.

#!/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

and a smaller one with:

#!/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!

I think I'm omitting the same thing on both. Both files are running only the last command.

Can someone enlighten me ?

Reference used

I've the help of everyone I was able to run the packages (=bash script filename) menu with inside:

#!/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

The two sh files contains each:

#!/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

Resulting in a working menu for all listed options: packages-bash script


For the installer-menu, I've tried to apply it so:

#!/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

This is how the menu shows up:

Installer-menu

But I forgot to enter every title between ' '

12
  • 1
    In your OPTIONS assignment, you appear to be confusing array construction ( ... ) with command substitution $( ... ) Jan 28, 2023 at 3:01
  • I do not realy understand, but I also think that it is on tntry RESULT=$ the problem. The menu doesn't even showup. It jumps directly to the last command. Any advice is welcome. @steeldriver
    – user330163
    Jan 28, 2023 at 3:06
  • I can see it's also used here, but do not understand it well yet: stackoverflow.com/questions/9084257/…
    – user330163
    Jan 28, 2023 at 3:08
  • so far as I can see, RESULT=$( ... ) is not a problem because you are trying to substitute the output of the dialog command Jan 28, 2023 at 3:14
  • @Wingarmac That's the syntax to create an array and to assign values to it, see Arrays.
    – Freddy
    Jan 28, 2023 at 3:15

1 Answer 1

0

Here's a (dry-run, echo-only) example of what I was talking about in my comments.

  1. use options=() instead of options=$() to create the array. Array lists and command substitution are not the same thing.
  2. multi-word elements of the array need to be quoted.
  3. lower-case variable names.

Also:

  1. Height and width in dialog are specified in characters, not pixels, so I've used 72x22 instead of 800x600. That's enough to fill most of a "standard" 80x25 screen or terminal. Alternatively, just set them both to 0 and dialog will make the menu as big as it needs to be.
  2. I've also renamed your variable CHOICE_HEIGHT to menu_height, mostly because that's how it's documented in the dialog man page.
  3. Double-quotes are for when you need to interpolate variables etc into strings. Single-quotes are better for fixed strings.
  4. I've added a "quit" option (in addition to the Cancel button provided by dialog --menu) and changed the case statement so that reboot isn't the default option. That's a terrible default, you don't want the machine to reboot if the user makes a mistake, or uses the Cancel button.

The most important items (1 & 2) above were already in the reference example on askubuntu.com that you started from, as was a width and height of 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
12
  • When I tried to launch it lke this, I get: packages: 10: Syntax error: "(" unexpected @cas
    – user330163
    Jan 29, 2023 at 21:49
  • 1
    Are you using #!/bin/sh (e.g. dash or ash or some ancient proprietary sh) instead of #!/bin/bash? Arrays aren't supported in posix-only sh, arrays are an enhanced feature that require enhanced shells like bash or ksh or zsh.
    – cas
    Jan 29, 2023 at 23:52
  • I'm feeling so silly now ... @cas
    – user330163
    Jan 30, 2023 at 9:59
  • For the treatment of the results, could you give a full line entry example for 1) echo while ... ? @cas
    – user330163
    Jan 30, 2023 at 10:04
  • I get these results packages: line 26: syntax error near unexpected token do' packages: line 26: do'when I choose for point ´3)` - Maybe I should put this command between ()? It isn't clear to me. @cas
    – user330163
    Jan 30, 2023 at 10:07

You must log in to answer this question.