Last active
February 14, 2021 07:17
-
-
Save projectivemotion/0ba0f4049cc28b4edff5865ff97b7958 to your computer and use it in GitHub Desktop.
Reads shell commands from an ini file and displays a dialog box allowing a user to select and execute any of the comands.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# github.com/projectivemotion | |
# | |
# This script displays a listbox and allows a user | |
# to select and execute any of the commands from a given | |
# ini file. (run.ini) | |
# | |
cd $(dirname "$0") | |
function read_ini(){ | |
if [ ! -f ./run.ini ] ; then | |
cat << EOF > ./run.ini | |
[hello-from-zenity] | |
run=zenity --info --text "Hello from Zenity" | |
title="Zenity" | |
[gedit] | |
run=gedit run.ini | |
title="Edit run.ini" | |
[sleep] | |
run=sleep 5 | |
title="Sleep for 5 seconds" | |
EOF | |
fi | |
section="" | |
while IFS='=]' read vleft vright | |
do | |
if [[ $vleft == \[* ]] ; then | |
section=${vleft/[/} | |
sections+=("$section") | |
elif [[ $vright ]] ; then | |
todeclare="section_${section}_$vleft" | |
vars[$todeclare]="$vright" | |
fi | |
done < ./run.ini | |
} | |
which zenity 2>&1 >/dev/null | |
if [[ 1 -eq $? ]] ; then | |
echo "Please install zenity." | |
exit 1 | |
fi | |
exitnow=0 | |
while [[ 0 -eq $exitnow ]]; do | |
declare -a sections | |
declare -A vars | |
read_ini | |
declare -a rows | |
for section in "${sections[@]}" ; do | |
run="section_${section}_run" | |
title="section_${section}_title" | |
rows+=("$section" "${vars[$title]}" "${vars[$run]}") | |
done | |
chosen=$(zenity --height 300 --width 800 --list \ | |
--title="Choose the command you wish to execute." \ | |
--column="Section" --column="Title" --column="Cmd" \ | |
--print-column=3 \ | |
"${rows[@]}") | |
exitnow=$? | |
if [[ 0 -eq $exitnow ]] ; then | |
echo $(date): $chosen | |
eval $chosen | |
fi | |
unset rows sections vars | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment