How to Show a Gui Message Box from a Bash Script in Linux

How to show a GUI message box from a bash script in linux?

I believe Zenity will do what you want. It's specifically designed for displaying GTK dialogs from the command line, and it's available as an Ubuntu package.

Installer on bash: how to show GUI messages?

Anything like xmessage or zenity or gxmessage implies external dependencies that you cannot guarantee will be available (unless you can; you haven't said so in your question). To answer one of your questions, NO, there is nothing universal for Linux. Certainly not anything that depends on X, since so many Linux installations are headless.

For "something else", as a general principle, being self-contained is a good idea. That means using something that doesn't even depend on the X Window System. Shell based dialogs are readily available, whether you're in FreeBSD or Linux.

To be truly self-contained as well as portable (even between different distros of Linux, or different server configurations), I'd suggest writing your own dialog manager as a function within your shell script. Something along the lines of this:

#!/usr/bin/env bash

# A supporting function to see test a value against the contents of an array
is_in() {
value=$1; shift
for i in "$@"; do [[ $i == $value ]] && return 0; done
return 1
}

# Simple dialog implementation, no VT100 required,
dialog() {
# $options is an array of options
local i line max=0
# determine dialog width
for line in "${options[@]}"; do [[ ${#line} -gt $max ]] && max=${#line}; done
# draw a line
eval printf '%.s-' {1..$((max+8))}; echo
# print each option
for i in ${!options[@]}; do
printf "| %2d: %-${max}s |\n" "$i" "${options[i]}"
done
eval printf '%.s-' {1..$((max+8))}; echo
response=""
# only accept valid responses
while ! is_in "$response" "${!options[@]}"; do
read -p " Choose: " response
done
return "$response"
}

# Create our list, run the dialog, capture the result,
options=([1]="Hello world" [2]="This is a test")
dialog
result=$?

# And display the result.
echo "RESPONSE: $result / ${options[$result]}"

Dialog from bash script

I've searched what dialog creators are. I found yad and with this I can set my desired options:

yad --skip-taskbar --center --title="Print dialog" {--image,--window-icon}=/usr/share/icons/Tango/72x72/devices/printer1.png --form --item-separator=, --field="Pages per sheet":CB 1,2,4,6,8 --field="Pages"

Dialog

And when I choose "2 pages per sheet" and pages "1-12" and after click OK the output will 2|1-12|.

This is what I desired. Zenity or Xdialog can do similar?

Bash shell script to prompt user for input with a (CLI GUI) dialog box

as sugested above dialog commands would work

Create a dialog with --keep-tite to keep your screen after input and using --checklist makes it posible to have radio select box.
Place all the answers into a array and have a while loop echo each array item.

${#arrayName[@]} --> number of items in array or number of selection
${arrayName[@]} --> outputs all array items

#!/bin/bash

result() {
i=0
echo there are ${#arrayName[@]} options selected
while (( i < ${#arrayName[@]} ))
do
echo $i ${arrayName[$i]}
i=$(( $i + 1 ))
done
}

cmd=(dialog --separate-output --keep-tite --checklist "Select options:" 22 76 4)
options=(1 "Johnny" off
2 "Ben" off
3 "Hillary" off
4 "User Input" off
)

choice=$("${cmd[@]}" "${options[@]}" 2>&1 > /dev/tty )

for answer in $choice
do
# make decsion
case $answer in
1)
arrayNum=${#arrayName[@]} # arrayNum is the amount of items in arrayName
arrayName[$arrayNum]="Johnny" # if selected put Johnny in arrayName
;;
2)
arrayNum=${#arrayName[@]} # If johnny is selected ${#arrayName[@]} outputs 1 if not selected 0
arrayName[$arrayNum]="Ben" # If selected add to array
;;
3)
arrayNum=${#arrayName[@]}
arrayName[$arrayNum]="Hillary"
;;
4) # If User Input is selected create an new dialog inputbox

user_input=$(\
dialog --keep-tite --title "Enter Your Name" \
--inputbox "Enter name:" 8 40 \
3>&1 1>&2 2>&3 3>&- \
)
arrayNum=${#arrayName[@]}
arrayName[$arrayNum]="$user_input"
;;
esac
done
result

Read -s with timeout shows text after timeout

I suggest using the -N option in order to circumvent read's behaviour that is to only consume full lines of input.

The description of -N starts as follows :

-N nchars

read returns after reading exactly nchars characters

rather than waiting for a complete line of input, unless

EOF is encountered or read times out.

Using a value large enough that your users wouldn't be able to reach it before the timeout seems to work well :

read -t 10 -a mp -s -N 9999 -p "Enter Password:"

A partial line will be stored into the array and won't be reproduced on your console's next line of input.



Related Topics



Leave a reply



Submit