mirror of
https://github.com/armbian/configng.git
synced 2026-01-06 10:37:41 -08:00
120 lines
4.1 KiB
Bash
Executable File
120 lines
4.1 KiB
Bash
Executable File
#!/bin/bash
|
|
|
|
#
|
|
# Copyright (c) 2023 Joseph C Turner
|
|
# All rights reserved.
|
|
#
|
|
# This script.
|
|
# demonstrates the compatibility of multiple interfaces for displaying menus or messages.
|
|
# It uses an array to set the options for all three menus (bash, whiptail, and dialog).
|
|
# The script checks if whiptail or dialog are available on the system and uses them to display the menu in a more user-friendly way.
|
|
# If neither of these programs is available, it falls back to using bash.
|
|
# while both are installed falls back to whiptail to display the menu.
|
|
# The user can override the default program by passing an argument when running the script:
|
|
#
|
|
# Redistribution and use in source and binary forms, with or without
|
|
# modification, are permitted provided that the following conditions are met:
|
|
#
|
|
# 1. Redistributions of source code must retain the above copyright
|
|
# notice, this list of conditions and the following disclaimer.
|
|
#
|
|
# 2. Redistributions in binary form must reproduce the above copyright
|
|
# notice, this list of conditions and the following disclaimer in the
|
|
# documentation and/or other materials provided with the distribution.
|
|
#
|
|
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
|
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
# POSSIBILITY OF SUCH DAMAGE.
|
|
#
|
|
|
|
## DIRECTORY variable to the absolute path of the script's directory
|
|
directory="$(dirname "$(readlink -f "$0")")"
|
|
filename=$(basename "${BASH_SOURCE[0]}")
|
|
|
|
## DIALOG variable to the absolute path of the script's directory
|
|
DIALOG="bash"
|
|
[[ -x "$(command -v dialog)" ]] && DIALOG="dialog"
|
|
[[ -x "$(command -v whiptail)" ]] && DIALOG="whiptail"
|
|
|
|
show_help(){
|
|
|
|
echo -e "\nUsage: [command] | ${filename%.*} [ -h | -m | -o ]"
|
|
echo "Options:"
|
|
echo " -h, Print this help."
|
|
echo ""
|
|
echo " -o, Opens an OK message Box"
|
|
echo ""
|
|
echo " -m, Opens an Menu select Box."
|
|
echo ""
|
|
echo " -p, Opens Popup message box. "
|
|
echo ""
|
|
exit 1;
|
|
}
|
|
|
|
show_message(){
|
|
|
|
# Read the input from the pipe continuously until there is no more input
|
|
input=""
|
|
while read -r line; do
|
|
input+="$line\n"
|
|
done
|
|
|
|
# Display the "OK" message box with the input data
|
|
[[ $DIALOG != "bash" ]] && $DIALOG --title "Message Box" --msgbox "$input" 0 0
|
|
[[ $DIALOG == "bash" ]] && echo -e "$input"
|
|
[[ $DIALOG == "bash" ]] && read -p "Press [Enter] to continue..." ; echo "" ; exit 1
|
|
|
|
}
|
|
|
|
show_popup(){
|
|
|
|
|
|
input=""
|
|
while read -r line; do
|
|
input+="$line\n"
|
|
done
|
|
|
|
[[ $DIALOG != "bash" ]] && $DIALOG --title "Popup Box" --infobox "$input" 0 0
|
|
[[ $DIALOG == "bash" ]] && echo -e "$input"
|
|
|
|
}
|
|
|
|
show_menu(){
|
|
|
|
|
|
# Get the input and convert it into an array of options
|
|
inpu_raw=$(cat)
|
|
# Remove the lines befor -h
|
|
input=$(echo "$inpu_raw" | sed 's/-\([a-zA-Z]\)/\1/' | grep '^ [a-zA-Z] ' | grep -v '\[')
|
|
options=()
|
|
while read -r line; do
|
|
package=$(echo "$line" | awk '{print $1}')
|
|
description=$(echo "$line" | awk '{$1=""; print $0}' | sed 's/^ *//')
|
|
options+=("$package" "$description")
|
|
done <<< "$input"
|
|
|
|
# Display the menu and get the user's choice
|
|
[[ $DIALOG != "bash" ]] && choice=$($DIALOG --title "Menu" --menu "Choose an option:" 0 0 9 "${options[@]}" 3>&1 1>&2 2>&3)
|
|
|
|
# Check if the user made a choice
|
|
if [ $? -eq 0 ]; then
|
|
echo "$choice"
|
|
else
|
|
echo "You cancelled."
|
|
fi
|
|
}
|
|
|
|
[[ $1 == "-m" ]] && show_menu ;
|
|
[[ $1 == "-o" ]] && show_message ;
|
|
[[ $1 == "-h" ]] && show_help ;
|
|
[[ $1 == "-p" ]] && show_popup ;
|
|
[[ -z "$@" ]] && show_help ;
|