Skip to main content
Question

Assistance with my shell script

  • November 7, 2017
  • 11 replies
  • 30 views

Forum|alt.badge.img+4

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

Forum|alt.badge.img+10
  • Valued Contributor
  • November 7, 2017

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


Forum|alt.badge.img+4
  • Author
  • New Contributor
  • November 7, 2017

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


Forum|alt.badge.img+7
  • Contributor
  • November 7, 2017

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

Forum|alt.badge.img+7
  • Valued Contributor
  • November 7, 2017

@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


Forum|alt.badge.img+4
  • Author
  • New Contributor
  • November 7, 2017

Will do - thanks so much!!


Forum|alt.badge.img+4
  • Author
  • New Contributor
  • November 7, 2017
#!/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?


Forum|alt.badge.img+7
  • Contributor
  • November 7, 2017

How do you have the policy configured?


Forum|alt.badge.img+4
  • Author
  • New Contributor
  • November 7, 2017

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


Forum|alt.badge.img+11
  • Valued Contributor
  • November 7, 2017

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


Forum|alt.badge.img+10
  • Valued Contributor
  • November 7, 2017

@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


Forum|alt.badge.img+16
  • Valued Contributor
  • November 7, 2017

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.