LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   [Whiptail checklist] Difference between 'nothing checked' and 'canceled' (https://www.linuxquestions.org/questions/programming-9/%5Bwhiptail-checklist%5D-difference-between-%27nothing-checked%27-and-%27canceled%27-4175733354/)

Michael Uplawski 01-30-2024 06:43 AM

[Whiptail checklist] Difference between 'nothing checked' and 'canceled'
 
Good afternoon.

Find below a (runnable) extract from a shell script, duplicating the one from https://www.linuxquestions.org/quest...es-4175733242/.

Here, I must learn to discern a situation, where a user does not deselect any item and another, where she/he has clicked 'Cancel' or pushed 'Esc'. I am currently unable to do so, as either *all* items are returned or none and cannot get a return-code (0 or 1 of all choices) or do not know how.

Of course this is all copied and pasted from the Web and other scripts, where I accomplish stuff which has nothing to do with the current task...

Script:
Code:

#!/bin/bash

options=(PP 'Post processing' ON GROUP_SIGS 'Signatures' ON CUSTOM_HEADERS 'Custom headers' ON XNAY_GROUPS 'X-No-Archive' ON DEBUG_LOG 'Log' ON)

eval opts=($(whiptail --title "$TITLE" --checklist "deselect to disable" 15 50 5 "${options[@]}" 3>&1 1>&2 2>&3))

echo "|${opts[@]}|"
echo "${#opts[@]}"


pan64 01-30-2024 07:31 AM

you can find some ideas here: https://en.wikibooks.org/wiki/Bash_S...pting/Whiptail
In general:
Code:

result=$(whiptail --title "$TITLE" --checklist "deselect to disable" 15 50 5 "${options[@]}" 3>&1 1>&2 2>&3)
retcode=$?
eval opts=($result)

may work

Michael Uplawski 01-30-2024 12:55 PM

Quote:

Originally Posted by pan64 (Post 6480248)
you can find some ideas here: https://en.wikibooks.org/wiki/Bash_S...pting/Whiptail
In general:
Code:

result=$(whiptail --title "$TITLE" --checklist "deselect to disable" 15 50 5 "${options[@]}" 3>&1 1>&2 2>&3)
retcode=$?
eval opts=($result)

may work

Darn. I even get a glimpse of what is happening. 8)
$? was in a previous version of the script, but in the current would not serve any purpose. I understand the call to eval (which is necessary but I forgot why after some dozen modifications) comes just too early.

Thank you very much. This is better than you may even guess.

Michael Uplawski 01-31-2024 03:11 PM

My current script still looks clumsy, but works (again).
For the intended use, I feed it a temporary file (from mktemp), a test can be made with just any writable or inexistent file as only argument, like in :
Code:

kong@bomb:~/ whiptail_dlg /tmp/test.out
Whiptail is only one alternative to other dialogs (Yad, zenity and a pure textmode menu), which all work as I want. I am not sure that this is worth further investment of time :

Code:

#!/bin/bash

#/**************************************************************************
#*  This program is free software; you can redistribute it and/or modify  *
#*  it under the terms of the WTFPL 2.0 or later, see                    *
#*  http://www.wtfpl.net/about/                                          *
#*                                                                        *
#*  This program is distributed in the hope that it will be useful,      *
#*  but WITHOUT ANY WARRANTY; without even the implied warranty of        *
#*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.                  *
#*                                                                        *
#***************************************************************************/

# A dialog to override configuration options.
# Whiptail is not as easily deployed here, as other dialogs.
# The main difficulty arises from the fact that positive defaults
# are negated, while a checklist wants to give positive results.
 
OUTFILE="$1"

# empty the file
truncate $OUTFILE -s0

TITLE="Override post-processor configuration"

# Apart from PP, these are the configuration variables which can be unset.
VARS=(PP GROUP_SIGS CUSTOM_HEADERS XNAY_GROUPS DEBUG_LOG)

# Checklist options
options=(PP 'Post processing' ON GROUP_SIGS 'Signatures' ON CUSTOM_HEADERS 'Custom headers' ON XNAY_GROUPS 'X-No-Archive' ON DEBUG_LOG 'Log' ON)

# show dialog and store results
result=$(whiptail --title "$TITLE" --checklist "deselect to disable" 15 50 5 "${options[@]}" 3>&1 1>&2 2>&3)
# which button had been pushed?
okcancel=$?

# make an array
read -ra opts <<< $result

# comment out -------->
#echo "cancel/ok ? $okcancel"
#echo $result
#echo "${opts[@]}"
#echo "${#opts[@]}"
# <--------

# Find deselected options which shall be written to the outfile.
# Looks complicated.
if [ "$okcancel" -eq 0 ]
then
                list=''
                # check each available variable ...
                for c in ${VARS[@]}
                do
                                deactivate=TRUE
                                # ... against the result from the dialog
                                for o in ${opts[@]}
                                do
                                                if [ "$o" == "\"$c\"" ]       
                                                then
                                                                # ignore if a variable remained checked ...
                                                                deactivate=FALSE
                                                fi
                                done
                                # ... else write it to the list.
                                if [ "$deactivate" == TRUE ]
                                then
                                               
                                                list="$list $c"
                                fi
                done
                # Heureka.
                echo "$list" > "$OUTFILE"
fi

# Ω


GazL 02-01-2024 07:35 AM

Woah, that double tab indentation level is a bit much. I couldn't cope working with that much whitespace.

Michael Uplawski 02-02-2024 12:54 AM

Quote:

Originally Posted by GazL (Post 6480660)
Woah, that double tab indentation level is a bit much. I couldn't cope working with that much whitespace.

Right.
This vim installation is not yet configured beyond the defaults. And I indented this way only before copy&pasting here. ;)

Wrong. The indentation rules for bash/sh scripts just do not conform. All other codes (Ruby) were okay. I have overruled with my own indentation settings in .vimrc, now.


All times are GMT -5. The time now is 02:27 PM.