I'm using Dialog, in bash, to create a simple multiple choice question. My options all include spaces in the values. When the Dialog is constructed, it appears to be building the array with a space delimiter.
My options are as such:
TITLE="Setup Script..." VER="0.2Β" { # Default Dialog Values. # └──These can be overwritten if need be. DIALOG_TOGGLE="OFF" # Used for dialog multiple choice. DIALOG_HEIGHT=15 # Sets the height. DIALOG_WIDTH=78 # Sets the width. DIALOG_CHOICES=4 # Sets the number of choices. DIALOG_OK_LABEL="Submit" # Sets the OK button label. DIALOG_CANCEL_LABEL="Cancel" # Sets the Cancel button label. DIALOG_EXTRA_BUTTON=true # Enable/Disable the extra button. DIALOG_EXTRA_LABEL="Select All" # Sets the "EXTRA" button label. DIALOG_BACKTITLE=$TITLE" v:"$VER # Sets the background title. DIALOG_TITLE="Example Title..." # Sets the dialog title. DIALOG_DESCRIPTION="\n\ Welcome to the $TITLE\n\n\ Example Description" declare -a DIALOG_OPTIONS=( "Example Item 1" "This is an example option" $DIALOG_TOGGLE "Example Item 2" "This is an example option" $DIALOG_TOGGLE "Example Item 3" "This is an example option" $DIALOG_TOGGLE ) } My Dialog is then constructed as such:
load_dialog() { # Check whether or not to display the extra button if [ "$DIALOG_EXTRA_BUTTON" = true ] ; then EB_TOGGLE='--extra-button' else EB_TOGGLE='' fi # Dialog Framework cmd=(dialog --colors --clear --keep-tite --backtitle "$DIALOG_BACKTITLE" --title "$DIALOG_TITLE" --ok-label "$DIALOG_OK_LABEL" --cancel-label "$DIALOG_CANCEL_LABEL" $EB_TOGGLE --extra-label "$DIALOG_EXTRA_LABEL" --checklist "${DIALOG_DESCRIPTION//$'\t'/}" "$DIALOG_HEIGHT" "$DIALOG_WIDTH" "$DIALOG_CHOICES" ) # Build Dialog CHOICES="$("${cmd[@]}" "${DIALOG_OPTIONS[@]}" 2>&1 1>/dev/tty)" # Detect the exit status of the Dialog (what button was pressed) exitStatus=$? # Backup existing IFS SAVEIFS=$IFS # Set new IFS IFS=$'\n' # Determine what choices were selected if [ -z "${CHOICES}" ]; then echo "No option was selected..." echo "└── User hit Cancel or unselected all options." else for CHOICE in ${CHOICES[@]}; do case $CHOICE in $CHOICE) echo "${CHOICE} enabled." ;; *) echo "Something went wrong!" ;; esac done fi # Restore backed up IFS IFS=$SAVEIFS case $exitStatus in 0) echo $DIALOG_OK_LABEL 'chosen'; ;; 1) echo $DIALOG_CANCEL_LABEL 'chosen'; exit ;; 3) echo $DIALOG_EXTRA_LABEL 'chosen'; DIALOG_TOGGLE=on # set all to on load_dialog ;; *) echo 'unexpected (ESC?)'; exit ;; esac } } I'm attempting to build this dialog as a sort of "constructor" so that I can pass it some different data, and build different dialogs on the fly. I'm attempting to not have to repeat the code of building the dialog each time. It's possible I may have a few prompts in my script along the way.
Ultimately, I'm trying to be able to differentiate the options picked by the user, as well as handling the exitstatus of the dialog when a button is selected.
As it stands now, when I run this, the CHOICES="$("${cmd[@]}"... seems to collect the values and build the array as such:
"Example Item 1" "Example Item 2" "Example Item 3" Assuming all 3 options are selected of course.
I want to be able to handle the array as such: (Maybe even as CSV, instead of SSV)
"Example Item 1" "Example Item 2" "Example Item 3" The issue I'm having, is trying to find a way to parse these items individually. I've been playing around with the IFS value, and that seems to work for ignoring the spaces in the values themselves, but from what I gather, this also then ignores the spaces separating the values themselves.
I'm a bit confused, and I've been stuck on this for a while now. I feel like I'm stuck in a catch 22 here. Any advice or help on how I can better handle this data?
IFSto. As long as it's set to something, say''or,'xxxxxxxx'or evenNULL, my for loop will catch theCHOICESarray as one long string. However, ifIFS=simply doesn't exist at all, it will loop through every single space in the array as a separate item. Including the space separated values, and the spaces within the values.}at the end ofload_dialogue${CHOICES[@]}. It should be"${CHOICES[@]}". Otherwise the spaces in the values will be treated as word delimiters.}is just a copy paste error, because I have theload_dialogfunction in a region defined above, which wasn't copied.