admin管理员组

文章数量:1384238

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
  • 4 Please paste your script at shellcheck and try to implement the recommendations made there. – Cyrus Commented Mar 18 at 21:03
  • What is your code echo $selection displaying? Does that ever change? – shellter Commented Mar 18 at 23:54
Add a comment  | 

1 Answer 1

Reset to default 1

If 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.

本文标签: applescriptIdentifying OSA button push with bashStack Overflow