0

I'm trying to set a variable based on user input. I know that what I have configured to prompt the user will either respond with "button returned:yes" or "button returned:no".

I would like the variable to be set to "True" if the button returns yes. and "False" if the button returns no.

This is what I have so far:

response="button returned:yes"
var=`/usr/bin/osascript -e 'display dialog "Is a reboot required?" buttons {No,Yes}'`
/bin/test "$response" = "$var" && reboot="True" || reboot="False"

What am I doing wrong? The $reboot variable is always setting to false, regardless of the user input. This is a bash script, intended to be run on a macOS system.

1

Adding this line to the end of a copy of your script:

echo "r: '$response'; v: '$var'; b: '$reboot'"

gives this output:

r: 'button returned:yes'; v: 'button returned:yes '; b: 'False'

It seems that osascript is appending a space to the output, and since "string" is not equal to "string ", your test fails to act as you expect.

  • Perfect! Thank you! I simply set $response to include a space at the end. – chippewa Jun 1 '17 at 16:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.