Whiptail: How to Redirect Output to Environment Variable

whiptail: How to redirect output to environment variable?

This is probably because whiptail uses stdin and stdout to print the input box, so you cannot redirect stderr directly to stdout, but you need to swap them, e.g:

foobar=$(whiptail --inputbox "Enter some text" 10 30 3>&1 1>&2 2>&3)

Capture command output into a local variable and check for error

Exit code of local password=$(...) is the status of the local command (which should ideally be 0). local is treated as a command and the return code of $(...) is overridden by that.

To avoid that, create the local variable first and then do the assignment.

i.e.

local password    # return code of this is always 0
password=`whiptail --clear \
--title "Password" \
--passwordbox "Enter your login password:" \
8 68 \
2>&1 >/dev/tty`
local err=$?

How can I treat the output of a command as a file?

You can use this process substitution syntax:

--textbox <(command) 600 800

Replace command with your custom command.

Bash incantation for defining whiptail radiolist programmatically

The following follows best practices set out in BashFAQ #50:

# note that lower-case variable names are reserved for application use by POSIX
# see https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap08.html
active_db="localhost"
dbs=( production localhost ) # using an array, not a string, means ${#dbs[@]} counts

# initialize an array with our explicit arguments
whiptail_args=(
--title "Select Database"
--radiolist "Select Database:"
10 80 "${#dbs[@]}" # note the use of ${#arrayname[@]} to get count of entries
)

i=0
for db in "${dbs[@]}"; do
whiptail_args+=( "$((++i))" "$db" )
if [[ $db = "$active_db" ]]; then # only RHS needs quoting in [[ ]]
whiptail_args+=( "on" )
else
whiptail_args+=( "off" )
fi
done

# collect both stdout and exit status
# to grok the file descriptor switch, see https://stackoverflow.com/a/1970254/14122
whiptail_out=$(whiptail "${whiptail_args[@]}" 3>&1 1>&2 2>&3); whiptail_retval=$?

# display what we collected
declare -p whiptail_out whiptail_retval

While I don't have whiptail handy to test with, the exact invocation run by the above code is precisely identical to:

whiptail --title "Select Database" \
--radiolist "Select Database:" 10 80 2 \
1 production off \
2 localhost on

...as a string which, when evaled, runs the precise command can be generated with:

printf '%q ' whiptail "${whiptail_args[@]}"; echo

Linux dialog user input directly to variable

dialog appears to be sensitive to option ordering. Put the --title option before the --inputbox option and it should work (at least it does here).

Bash - Can't copy associative array if piping block output to command

Would you please try the following:

#!/bin/bash

declare -Ar ARR1=(
[a]='asdf'
[b]='qwerty'
[c]='yuio'
)

declare -A ARR2=()

clone() {
{
n=${#ARR1[*]} # number of items
for key in "${!ARR1[@]}"; do
ARR2[$key]="${ARR1[$key]}"
(( i++ )) # increment a counter
echo $(( 100 * i / n )) # percentage
sleep 1 # wait for 1 sec
done
} > >(whiptail --gauge "Cloning" 6 60 0)
}

clone

for key in "${!ARR2[@]}"; do
echo "$key"
echo "${ARR2[$key]}"
done

The key is the } > >(whiptail ... expression which keeps the enclosed block in the foreground process without using a pipeline.

Please note that I have modified the code to display percentage to make it look like that.

Error with Linux whiptail/dialog arguments from bash variable

The answer from the comments if somebody comes across this.

TEST=(M1 '1-wire Interface' ON)
TEST=( "${TEST[@]}" M2 'Other Interface' OFF )
echo ${TEST[@]}
dialog --title "Config Modules State" --checklist "Choose modules to activate" 20 50 2 "${TEST[@]}"


Related Topics



Leave a reply



Submit