Assistance with my shell script

SJBradleyNY
New Contributor II

Hey Guys,

I'm wondering if anyone can show me what I'm doing wrong? I wrote a Shell script to force a computer to restart (Using admin details). It works fine when I test it but when running it through Self Service on another computer I receive the error that "It could not be installed"

I feel like I'm missing something obvious:

!/Bin/Bash/

set username to "MyAdminUsername"
set passwd to "MyPassword"

tell application "Finder" activate display dialog "Thank you for using Fix My Mac - If you still have problems after the restart, contact I.T." buttons {"I'm ready to restart"} default button 1

do shell script "shutdown -r now" user name username password passwd with administrator privileges

end tell
end

11 REPLIES 11

Asnyder
Contributor III

You're using AppleScript syntax but you're using it in a shell script. If you want to use AppleScript it should be scriptname.script. If you want to use applescript within a bash script you need to use the command

osascript -e

OR various other formats like so:

#!/bin/bash
osascript << EOF
tell application "Terminal"
activate
end tell
EOF

EDIT: Self service script run as root so no need for admin credentials

SJBradleyNY
New Contributor II

Thanks for getting back to me. I had a feeling that the AppleScript was causing an issue. I'm a newbie to this. To clarify is this what it should look like?

!/Bin/Bash/

osascript -e

tell application "Finder" activate display dialog "Thank you for using Fix My Mac - If you still have problems after the restart, contact I.T." buttons {"I'm ready to restart"} default button 1

do shell script "shutdown -r now" end tell
end

tlagrange
New Contributor III

When submitting code, use the code snippet prompt! Makes reading the code much easier.

#!/bin/bash/

osascript << EOF

tell application "Finder"
  display dialog "Thank you for using Fix My Mac - If you still have problems after the restart, contact I.T." buttons {"I am ready to restart"} default button 1
  do shell script "shutdown -r now"
end tell

EOF

osxadmin
Contributor II

@SJBradleyNY for future reference when you want to post a script use the ">_" see screen shot for the example:

#!/bin/bash
osascript << EOF
tell application "Terminal"
activate
end tell
EOF

b9852f45e1c5407199e68f16a681e2bc

SJBradleyNY
New Contributor II

Will do - thanks so much!!

SJBradleyNY
New Contributor II
#!/bin/sh

osascript << EOF

tell application "Finder"
  display dialog "Thank you for using Fix My Mac - If you still have problems after the restart, contact I.T." buttons {"I am ready to restart"} default button 1
  do shell script "shutdown -r now"
end tell

EOF

When self service runs this I still get the cannot be installed message. Should it be running as a .sh file?

tlagrange
New Contributor III

How do you have the policy configured?

SJBradleyNY
New Contributor II

Uploading the shell script into Casper Admin and then have it set up to only be accessible through self service.

znilsson
Contributor II

The script runs as root when run from Self Service - does it need to be run as the logged-in user instead?

Asnyder
Contributor III

@znilsson Since it's displaying a dialog I believe you're correct. I think it needs to run as the logged in user.

@SJBradleyNY The file extensions should be .sh since you're keeping as a shell file. If you were just making it an AppleScript file it would be .script

Look
Valued Contributor III

There is no need to loop into osascript and back to shell, move the

shutdown -r now

Outside of the EOF tags and run it directly as part of the initial shell script, it probably works the way it is, it's just a bit messy.