do shell script with administrator privileges & shell commands
do shell script with administrator privileges & shell commands
- Subject: do shell script with administrator privileges & shell commands
- From: Chris Espinosa <email@hidden>
- Date: Tue, 13 Aug 2002 16:15:53 -0700
After working with a couple of different groups on this issue we've
discovered an awkwardness with the 'with administrator privileges'
option for the 'do shell script' command. We've found a workaround so
I'm writing this mini-tech note to let people know about it.
PROBLEM
'do shell script' executes its argument in a one-time process which
quits after it's complete, so you can't string together multiple 'do
shell script' commands that rely on the results of previous commands
(like setting working directories). So scripters are encouraged to do
multiline scripts in one 'do shell script' command like this:
do shell script "cd /Users" & (ascii character 10) & "pwd"
(ascii character 10 is the UNIX newline, which separates commands).
Unfortunately, the 'with administrator privileges' option only works on
UNIX commands that are implemented as files in the file system, not
those that are built in to the shell's command language. So multiline
scripts like the above will fail when used 'with administrator
privileges' if they include any shell commands.
SOLUTION
The solution is to write your entire shell script to a temporary file
and execute that file using 'do shell script', like so:
property newline : ASCII character 10
property tmpfile : "/tmp/execme"
property theShellScript : "cd /Users" & newline & "pwd" & newline &
"id" & newline
do shell script "echo " & quoted form of theShellScript & " > " &
tmpfile
do shell script "chmod +x " & tmpfile
do shell script tmpfile with administrator privileges
The 'tmpfile' property can be any path (we use the /tmp directory
because it's cleaned up routinely) and the 'theShellScript' property
should be the lines of your shell script separated by UNIX newline
characters. The script above simply writes the shell script to a
temporary file, makes it executable, and executes it with administrator
privileges. This way the script can contain both shell commands and
file-based commands.
NOTES
The most common problems with do shell script:
1) To conform to UNIX conventions, it uses /bin/sh as the shell. This
shell operates differently from the default shell (/bin/tcsh) used by
the Terminal, so you can't necessarily bring shell scripts straight
across from Terminal; you need to restrict your scripts to the
capabilities of /bin/sh
2) Environment variables that are set in your user login (like your
$PATH) are not pre-set for 'do shell script', so you may need to
specify explicit paths for some commands (like those in
/Developer/Tools).
3) 'do shell script' commands don't follow your normal user login
process; for example, the working directory is "/" (root), not your
user directory, so that scripts are more portable. You need to plan
for this in the shell scripts you execute.
4) As noted above, each 'do shell script' command is a separate
process, so you can't rely on context set in a previous command to be
in effect for subsequent ones.
5) It will wait until the command is complete and standard output and
standard error are closed before returning. If you try to use it to
execute a command in the background, you have to redirect standard out
and standard error in order to proceed with AppleScript execution:
do shell script "command > /dev/null 2>&1 &"
(The > /dev/null redirects standard output, the 2>&1 redirects
standard error, and the final & puts the command in the background.)
6) It doesn't set up terminal properties, so certain commands (like
'top') will not work unless they have command-line options that let
them work in a pipe or spooling to a file.
Chris Espinosa
Apple
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.