LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-30-2024, 06:43 AM   #1
Michael Uplawski
Senior Member
 
Registered: Dec 2015
Posts: 1,622
Blog Entries: 40

Rep: Reputation: Disabled
[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[@]}"

Last edited by Michael Uplawski; 01-30-2024 at 06:46 AM. Reason: elokwentz II
 
Old 01-30-2024, 07:31 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,857

Rep: Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311
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
 
1 members found this post helpful.
Old 01-30-2024, 12:55 PM   #3
Michael Uplawski
Senior Member
 
Registered: Dec 2015
Posts: 1,622

Original Poster
Blog Entries: 40

Rep: Reputation: Disabled
Quote:
Originally Posted by pan64 View Post
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.

Last edited by Michael Uplawski; 01-30-2024 at 12:56 PM. Reason: orthocraphucs.
 
Old 01-31-2024, 03:11 PM   #4
Michael Uplawski
Senior Member
 
Registered: Dec 2015
Posts: 1,622

Original Poster
Blog Entries: 40

Rep: Reputation: Disabled
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

# Ω
 
Old 02-01-2024, 07:35 AM   #5
GazL
LQ Veteran
 
Registered: May 2008
Posts: 6,897

Rep: Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019
Woah, that double tab indentation level is a bit much. I couldn't cope working with that much whitespace.
 
Old 02-02-2024, 12:54 AM   #6
Michael Uplawski
Senior Member
 
Registered: Dec 2015
Posts: 1,622

Original Poster
Blog Entries: 40

Rep: Reputation: Disabled
Quote:
Originally Posted by GazL View Post
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.
 
  


Reply

Tags
cancel, checklist, return code, whiptail



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Below is boot log ,I m able to see rootfs mounted but it is somehow stucking with the error -8, i did checked the init.txt but nothing help PranaliL Linux - Kernel 1 03-26-2021 05:44 PM
Kubuntu - can't get past login screen and whiptail error slav0j Linux - Laptop and Netbook 4 02-06-2018 02:36 PM
Ubuntu 18.04 To Ship with GNOME Desktop, Not Unity. Ubuntu phone and convergence shell have been canceled. jeremy Linux - News 6 04-20-2017 08:23 AM
Exam fee requested (by Redhat) as a monthly payment. Exam is canceled by redhat and I already paid for it twice. gerko Red Hat 7 01-09-2016 08:09 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration