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 eval
ed, 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
Ssh: Could Not Resolve Hostname [Hostname]: Nodename Nor Servname Provided, or Not Known
Copy Folder Structure (Without Files) from One Location to Another
How to Remove All Non-Numeric Characters from a String in Bash
Crontab Run Every 15 Minutes Between Certain Hours
How to Launch a New Process That Is Not a Child of the Original Process
How to Connect to a Terminal to a Serial-To-Usb Device on Ubuntu 10.10 (Maverick Meerkat)
Linux Shell Script for Each File in a Directory Grab the Filename and Execute a Program
Use Grep to Find Content in Files and Move Them If They Match
Using Rsync Include and Exclude Options to Include Directory and File by Pattern
Read Line by Line in Bash Script
Run Bash Command on Jenkins Pipeline
Location of .Bashrc for "Bash on Ubuntu on Windows" in Windows 10
How to Use Expect with Optional Prompts
Script to Change Password on Linux Servers Over Ssh
Bypass Rsync Prompt "Are You Sure You Want to Continue Connecting"