[SOLVED] [Whiptail checklist] Difference between 'nothing checked' and 'canceled'
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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[@]}"
Last edited by Michael Uplawski; 01-30-2024 at 06:46 AM.
Reason: elokwentz II
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.
Last edited by Michael Uplawski; 01-30-2024 at 12:56 PM.
Reason: orthocraphucs.
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
# Ω
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.
Last edited by Michael Uplawski; 02-02-2024 at 09:43 AM.
Reason: Right/Wrong ... indentation rules. strike! Not striike.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.