combine shell scripts with administrator privileges

Sarah Reichelt sarah.reichelt at gmail.com
Fri Jul 11 07:32:46 EDT 2008


On Fri, Jul 11, 2008 at 5:15 PM, Scott Morrow
<scott at elementarysoftware.com> wrote:
> I'm trying to combine 2 shell scripts and my knowledge of shell scripting
> (and how to combine it with AppleScript) is proving too minimal.
>
> I can do this:
>
> put  "chmod 777"&& tInnerPath into tShellCmd
> put "do shell script" &&quote& tShellCmd &quote&& "with administrator
> privileges" into tCmd
> do tCmd as applescript
>
> What I want to do is piggyback a second script to chmod another file while
> making use of the previous admin privileges
>
> (Otherwise I end up with a second dialog box asking the user to enter the
> password again)

If you want the standard OS X admin privileges dialog to appear, then
you will have to do it the way you are going and just live with having
two dialog boxes. A lot of apps do this anyway e.g. TextWrangler.

However if you are prepared to ask for an admin password using a
standard Rev dialog, then you can forget about AppleScript and just
use the shell function directly.

If the command does not require admin privileges, then you can just do this:

put  "chmod 777"&& tInnerPath into tShellCmd
get shell(tShellCmd)

If it requires admin privileges, then you will have to ask for the
password, store it in a variable (tPass) and then combine it:

put "#!/bin/sh" & cr into tScript
put "pw=" & quote & tPass & quote & cr after tScript
put "echo $pw | sudo -S " & tShellCmd & cr after tScript
put shell(tScript) into tCheck  -- do the command & get the result

In this case, once you have the password, you can do as many shell
commands as you want.

Just one warning: if you are using file paths in a shell command, you
will have a problem if there are any spaces in the folder or file
name. Either surround the file path by quotes or escape the spaces. To
see how this can be done, open the terminal and drag a file into the
Terminal window. This will give you the full file path, escaped as the
shell needs it. However I find it easier just to use quotes.

Cheers,
Sarah



More information about the use-livecode mailing list