I need to display a GUI pop up that shows a message with a variable, and allows user to confirm or cancel.
Perhaps I'm missing something, but I could not find any documentation on Zenity --questions tag. So far I couldn't get anything other than default question to display.
I don't want to use Yad if possible... I would like to stick with Zenity.
Is there any way to do this with Zenity?
Example:
var="ABC"
msg="Confirm $var"
zenity --text-info \
--title="Confirm"
--text="$msg"
# --question "This is the question"
case $? in
0)
return
;;
1)
return
;;
esac
zenity --question --title="Confirm" --text="$msg"
It's --question, not --questions, but in the Zenity manual, do a Ctrl-F for question to find all the relevant sections.
Related
I have a requirement where ScriptA.sh has commands to ask for User's inputs and perform a set of actions. I want to automate this by creating another script which will read the questions asked from output of ScriptA.sh and provide the necessary values in runtime.
ScriptA.sh as follows :-
echo "Enter the CR Number"
read varnamecr
echo "CR Number is" $varnamecr
echo "Loading the config set. Choose Option From Below set
1.JAN
2.FEB
3.MAR"
read optionchoosen
echo "Option Choosen is :" $optionchoosen
echo "Will run the script/load configuration is this Ok ?[y/N]"
read userinput
echo "Proceed further, User has pressed ->"$userinput"<--Key"
How to write the second script to achieve this. Tried spawn and few other commands in the second script, but no luck. Please help me with this.
Since you're not specifying any shell in your tag, this is a possible, albeit crude, solution in ksh. It's using the coprocess capability of that shell (pretty sure it's not supported in bash although please don't quote me on that one)
#!/bin/ksh
./ScriptA.sh |&
while read -p Dummy; do
print $Dummy
case $Dummy in
"Enter the CR Number")print -p "CR123456"
;;
"3.MAR")print -p "3"
;;
"Will run the script"*)print -p "y"
;;
esac
done
The output gives :
Enter the CR Number
CR Number is CR123456
Loading the config set. Choose Option From Below set
1.JAN
2.FEB
3.MAR
Option Choosen is : 3
Will run the script/load configuration is this Ok ?[y/N]
Proceed further, User has pressed ->y<--Key
Will input remain same everytime? If so you can create wrapper of this script to provide required input.
cat wrapper
./ScriptA.sh <<!
123
2
y
!
I am playing around with YAD dialogs in BASH and am having trouble with the button construction. I can't get a YAD button to call a function in the same script. Is there a way to do this?
My understanding is the if I use the following construction, pressing the button will call the command that follows the colon This example (which works) will open an instance of Firefox if a user clicks the Open Browser button:
yad --button="Open browser":firefox
I have a script with several BASH functions. I'd like a button press to call one of the functions. It doesn't. The following is a simple script that, when run, demonstrates the disappointing behavior:
#!/bin/bash,
click_one()
{
yad --center --text="Clicked the one"
}
click_two()
{
yad --center --text="Clicked the two"
}
cmd="yad --center --text=\"Click a button to see what happens\" \
--button=\"One\":click_one \
--button=\"Two\":2 \
--button=\"Date\":date \
--button=\"Exit\":99"
proceed=true
while $proceed; do
eval "$cmd"
exval=$?
case $exval in
2) click_two;;
99) proceed=false;;
esac
done
In the code above, button Date works as expected, calling the date command. Buttons Two and Exit work because I'm checking the exit value of the command and branching on its value. Sadly (for me), button One does nothing. I had hoped that clicking on button One would call the local function click_one. I would like to know if there's a way to format the YAD command so that the click_one function is called.
While the above code suggests a workaround using the exit value, my real goal is to apply a successful answer to a form button that, as far as I can figure out so far, doesn't return an exit value. In other words, the following also fails silently, but I'd like it to invoke function click_one:
yad --form --field="One":fbtn click_one
a possible way:
#!/bin/bash
click_one(){
yad --center --text="Clicked the one"
}
click_two(){
yad --center --text="Clicked the two"
}
export -f click_one click_two
yad \
--title "My Title" \
--center --text="Click a button to see what happens" \
--button="One":"bash -c click_one" \
--button="Two":"bash -c click_two" \
--button="Date":"date" \
--button="Exit":0
echo $?
Apparently not, it needs to be an actual command.
You could: put your functions in a separate file and as the command, launch bash, source that file, and call the function.
Here, I'm also restructuring your code to store the yad command in an array. This will make your script more robust:
# using an array makes the quoting a whole lot easier
cmd=(
yad --center --text="Click a button to see what happens"
--button="One":"bash -c 'source /path/to/functions.sh; click_one'"
--button="Two":2
--button="Date":date
--button="Exit":99
)
while true; do
"${cmd[#]}" # this is how to invoke the command from the array
exval=$?
case $exval in
2) click_two;;
99) break;;
esac
done
Hi everyone and thanks in advance.
I have a question for you.
In a shell script that I made to manage the startup/shutdown of a Jboss application cluster I use a function to answer various question during the process which tells the script wath to do
function ThreeOptions
{
ACT=0
read -n 1 -s answ
case ${answ:0:1} in
n|N) echo "Operation skipped"
ABRT=2
;;
e|E|x|X) echo "Operation aborted"
ABRT=3
CloseProcedure
;;
y|Y|*) echo "...ok..."
ACT=1
;;
esac
}
The function is written that way so that single pressure of the letter will select the option, and "Enter/Return" (well every other key indeed) will use the default.
How can I prevent the read command from reading unintentionally double/multiple keyboard inputs (aka sloppy hands).
EX.
echo -en " Shall I close all Jboss processes ? [Y/n/e]: "
ThreeOptions
...
echo -en " Shall I do some other thing?"
ThreeOptions
...
If I press "yy" I notice that I'm answering the ACTUAL question and the NEXT too.
PS: First question, so I hope it's understandable and that I didn't write too much :)
If you are using bash you can try the shell pattern matching in the bash case structure.
For example use +([y])|+([Y])|*) instead of y|Y|*)
Edit: remember to call shopt -s extglob in the function before using the case.
I'm on macOS. I have a script that, after asking for confirmation using read in Terminal, uses grep to check whether /dev/disk1 is mounted, then formats that disk. That's a dangerous script, hence why asking if it's okay first is vital.
Eventually, I would like to have this script be an executable that the user can double-click on. But rather than having the user type "y" and Return into a Terminal window, I would rather a display dialog appear with "yes" and "no" buttons, have them choose, then have the script run based on their answer. Is this possible in bash?
I'm working in an environment in which I don't have administrative access, so while I can write an AppleScript Service to accomplish what I want to do and to integrate this elegantly into the user interface, I can't integrate that Service into the environment without the admin password (since I can't edit ~/Library/Services for the user without it). Also, I cannot download or install any new libraries, applications — anything, really — in the environment; I must only use native bash in Mac OS X.
Here is what I have:
read -p "Are you sure you want to partition this disk? " -n 1 -r # Can I make this be a dialog box instead?
echo
if [[ $REPLY =~ ^[Yy]$ ]] # Can this accept the result as a condition?
then
if grep -q 'disk1' /dev/ && grep -q 'file.bin' ~/Downloads; then
echo # redacted actual code
else
osascript -e 'tell app "System Events" to display dialog "The disk is not mounted."'
exit 1
fi
else
exit 1
fi
Thanks very much for your help.
Yes, it is possible in bash to take the output of an osascript dialog. Here’s an example with a Yes/No dialog box:
#!/bin/bash
SURETY="$(osascript -e 'display dialog "Are you sure you want to partition this disk?" buttons {"Yes", "No"} default button "No"')"
if [ "$SURETY" = "button returned:Yes" ]; then
echo "Yes, continue with partition."
else
echo "No, cancel partition."
fi
If you run this script, the script should echo the appropriate line depending on which button was pressed.
It also shows how to set the default button, which I am assuming for the example is “No”.
If you have a more complex dialog, you would most likely use a regex to detect the responses, as you do in your own sample; though depending on your use case you might want to guard against spoofing responses.
If you're happy with a text-mode (but cross-platform and proven) solution, try using ncurses, and in particular a utility called dialog.
dialog --yesno "Are you sure you want to partition this disk?" 5 50
answer=$? # Returns: 0 == yes, 1 == no
More details in this tutorial.
I want to create simple .sh script which will include binary package with my program and will copy it to destination folders. During Installation I want to show gui messages to imform user that all is ok. Seems like zenity from this question is that I need How to show a GUI message box from a bash script in linux?
But how to supply it with my single .sh script? (User should run installer from anywhere without any additional actions). Is there something universal for most common distributions of linux? Maybe "xmessage", but it looks very poor. Something else?
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]}"