Skip to content

Instantly share code, notes, and snippets.

@mslinn
Last active May 1, 2019 01:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mslinn/0765e3eaaf90ce03f751384933eefa44 to your computer and use it in GitHub Desktop.
Save mslinn/0765e3eaaf90ce03f751384933eefa44 to your computer and use it in GitHub Desktop.
Bash incantation for defining whiptail radiolist programmatically
{
"activeDatabase": "localhost",
"databases": [
{
"name": "production",
"PGDB": "asdf",
"PGHOST": "asfd",
"PGPASSWORD": "asf",
"PGPORT": "asdf",
"PGUSER": "asf"
},
{
"name": "localhost",
"PGDB": "asf",
"PGHOST": "asf",
"PGPASSWORD": "asf",
"PGPORT": "asdf",
"PGUSER": "asf"
}
]
}
$ scConfig
ACTIVE_DB="$( jq -r '.activeDatabase' < "$HOME/.cadenzaClient.json" )"
::39+jq -r .activeDatabase
:39+ACTIVE_DB=localhost
info "Active database is $ACTIVE_DB"
:40+info 'Active database is localhost'
:30+printf 'Active database is localhost\n'
Active database is localhost
readarray -t dbs < <( jq -r '.databases[]|.name' < "$HOME/.cadenzaClient.json" )
:42+readarray -t dbs
jq -r '.databases[]|.name' < "$HOME/.cadenzaClient.json"
::42+jq -r '.databases[]|.name'
DB_COUNT="$( echo "${dbs[@]}" | wc -w )"
::43+echo production localhost
::43+wc -w
:43+DB_COUNT=2
info "The $DB_COUNT databases are:\n${dbs[@]}"
:44+info 'The 2 databases are:\nproduction' localhost
:30+printf 'The 2 databases are:\nproduction\n'
The 2 databases are:
production
# initialize an array with our explicit arguments
whiptail_args=(
--title "Select Database"
--radiolist "Select Database:"
10 80 "${#dbs[@]}"
)
:51+whiptail_args=(--title "Select Database" --radiolist "Select Database:" 10 80 "${#dbs[@]}")
i=0
:53+i=0
for db in "${dbs[@]}"; do
whiptail_args+=( "$((++i))" "$db" )
if [[ $db = "$ACTIVE_DB" ]]; then
whiptail_args+=( "on" )
else
whiptail_args+=( "off" )
fi
done
:54+for db in "${dbs[@]}"
:55+whiptail_args+=("$((++i))" "$db")
:56+[[ production = \l\o\c\a\l\h\o\s\t ]]
:59+whiptail_args+=("off")
:54+for db in "${dbs[@]}"
:55+whiptail_args+=("$((++i))" "$db")
:56+[[ localhost = \l\o\c\a\l\h\o\s\t ]]
:57+whiptail_args+=("on")
# collect both stdout and exit status
whiptail_out=$(whiptail "${whiptail_args[@]}" 3>&1 1>&2 2>&3 ); whiptail_retval=$?
::64+whiptail --title 'Select Database' --radiolist 'Select Database:' 10 80 2 1 production off 2 localhost on
:64+whiptail_out=
:64+whiptail_retval=255
# display what we collected
declare -p whiptail_out whiptail_retval
:67+declare -p whiptail_out whiptail_retval
declare -- whiptail_out=""
declare -- whiptail_retval="255"
echo "whiptail_out=$whiptail_out"
:69+echo whiptail_out=
whiptail_out=
echo "whiptail_retval=$whiptail_retval"
:70+echo whiptail_retval=255
whiptail_retval=255
#!/bin/bash
# See https://stackoverflow.com/a/55930193/553865
#PS4=':$LINENO+'
#set -xv
ACTIVE_DB="$( jq -r '.activeDatabase' < "$HOME/.cadenzaClient.json" )"
info "Active database is $ACTIVE_DB"
readarray -t dbs < <( jq -r '.databases[]|.name' < "$HOME/.cadenzaClient.json" )
DB_COUNT="$( echo "${dbs[@]}" | wc -w )"
info "The $DB_COUNT databases are:\n${dbs[@]}"
# initialize an array with our explicit arguments
whiptail_args=(
--title "Select Database"
--radiolist "Select Database:"
10 80 "${#dbs[@]}"
)
i=0
for db in "${dbs[@]}"; do
whiptail_args+=( "$((++i))" "$db" )
if [[ $db = "$ACTIVE_DB" ]]; then
whiptail_args+=( "on" )
else
whiptail_args+=( "off" )
fi
done
# collect both stdout and exit status
whiptail_out=$(whiptail "${whiptail_args[@]}" 3>&1 1>&2 2>&3 ); whiptail_retval=$?
# display what we collected ... comment for production
declare -p whiptail_out whiptail_retval
if [ $whiptail_retval == 255 ]; then exit 1; fi
#echo "whiptail_out=$whiptail_out"
echo "${dbs[whiptail_out-1]}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment