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 ?
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:
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:
But I forgot to enter every title between ' '
OPTIONS
assignment, you appear to be confusing array construction( ... )
with command substitution$( ... )
– steeldriver Jan 28 '23 at 03:01RESULT=$
the problem. The menu doesn't even showup. It jumps directly to the last command. Any advice is welcome. @steeldriver – Jan 28 '23 at 03:06RESULT=$( ... )
is not a problem because you are trying to substitute the output of thedialog
command – steeldriver Jan 28 '23 at 03:14OPTIONS=( ... )
(without the $) for creating an array. You'll also need to quote each multi-word array element individually, otherwise each word will become a separate element due to the shell's whitespace word-splitting). e.g.OPTIONS=(1 "Add Mint PPA and update" 2 "Install Cinnamon" ...)
. And remember that dialog's --menu option requires tag (e.g. 1, 2, ...) and item pairs ("Add Mint PPA...", "Install Cinnamon", etc). – cas Jan 28 '23 at 13:35OPTIONS=(...)
is for defining an array called OPTIONS.RESULT=$(...)
is for executing a program and storing the output in a variable called RESULT (this is called command substitution). BTW, it's generally best not to use all-caps for your own variable names - by convention, all-caps names are for the shell internal variables and exported variables. Best to useoptions
andresult
instead. – cas Jan 28 '23 at 13:40sh -c 'echo ...'
? You don't need to run echo in a sub-shell, that could be written simply as, e.g.,echo "deb http://packages.linuxmint.com vanessa main upstream import backport romeo" >> /etc/apt/sources.list.d/mint.list
. Also, if the*.sh
scripts being called from the various case statements were executable and had an appropriate#!
line, you could just run them as./script-name.sh
rather thansh script-name.sh
. And, worth noting, if any of them use arrays or other bash features then they'd need to be run with bash, not sh. – cas Jan 31 '23 at 03:11;
at the end of a line. As with any shell commands, on the command-line or in a script, you can separate them with either a;
or a newline. An extra;
doesn't harm anything, but it's not required. The;;
to terminate a case IS required, though, whether there's one command or more. – cas Jan 31 '23 at 03:14;
,&
,>
,|
and many more) ALL need to be either escaped or quoted. This is not specific to arrays, it's the same whenever you want to use a shell metacharacter as a literal string. If it's not quoted or escaped, the shell will act on it. if it is quoted, the shell will treat it as just another character. – cas Jan 31 '23 at 03:18