I am trying to determine which button was pushed in an OSA generated dialog using bash conditionals. However, no matter which button is pushed, the restart command always runs. Any thoughts?
#!/bin/sh
selection=$(osascript -e 'display dialog "A restart is required for this software to install properly..." buttons {"Test", "Restart Later", "Restart Now"} default button "Restart Now"')
echo $selection
if [[ $selection="button returned:Restart Now" ]]; then
echo "Would Restart..."
#sudo shutdown -r now
elif [[ $selection="button returned:Restart Later" ]]; then
$(osascript -e 'display dialog "Software was not installed." buttons {"Ok"} default button "Ok" with icon caution')
else
echo "Nothing selected!"
fi
exit 0;
I am trying to determine which button was pushed in an OSA generated dialog using bash conditionals. However, no matter which button is pushed, the restart command always runs. Any thoughts?
#!/bin/sh
selection=$(osascript -e 'display dialog "A restart is required for this software to install properly..." buttons {"Test", "Restart Later", "Restart Now"} default button "Restart Now"')
echo $selection
if [[ $selection="button returned:Restart Now" ]]; then
echo "Would Restart..."
#sudo shutdown -r now
elif [[ $selection="button returned:Restart Later" ]]; then
$(osascript -e 'display dialog "Software was not installed." buttons {"Ok"} default button "Ok" with icon caution')
else
echo "Nothing selected!"
fi
exit 0;
Share
Improve this question
asked Mar 18 at 20:51
ezoggezogg
1
2
|
1 Answer
Reset to default 1If you are using bash constructs like [[
, why do you use /bin/sh
in the shebang line? Depending on OS, this could be bash (3.2 on MacOS, 5 on Ubuntu), or dash on Debian, or something else. The shell may even check what it's called as, and switch into some compatibility mode. But you clearly want bash, so just say that.
Then shellcheck will point out the next issue:
if [[ $selection="button returned:Restart Now" ]]; then
^-- SC3010 (warning): In POSIX sh, [[ ]] is undefined.
^-- SC2077 (error): You need spaces around the comparison operator.
Fixing these issues gets me this minimal example:
#!/usr/bin/env bash
selection=$(osascript -e 'display dialog "A restart is required for this software to install properly..." buttons {"Test", "Restart Later", "Restart Now"} default button "Restart Now"')
if [[ $selection = "button returned:Restart Now" ]]; then
echo "Would Restart..."
fi
When I run this and click the "Restart Now" button, I get the expected "Would Restart..." message; when I click any other button I don't get a message, as expected.
I'd recommend you also fix the other shellcheck problems, for good style.
echo $selection
displaying? Does that ever change? – shellter Commented Mar 18 at 23:54