Re: read shell into property, pass to do shell script?
Re: read shell into property, pass to do shell script?
- Subject: Re: read shell into property, pass to do shell script?
- From: Ron Bishop <email@hidden>
- Date: Fri, 3 Oct 2003 12:13:59 -0500
On Friday, October 3, 2003, at 09:42 AM, Star wrote:
Hello,
How is the best way to read a shell script into a
property, therefore embedding it, and then pass it to
do shell script without using the original text/shell file?
Info below is where I'm at and what I've been trying
including how I have tried to read it in.
This may or may not help, since I'm arriving late to the party, but
here is how I pass shell scripts to Applescript and have had no
problems. This is based on a posting by Chris Espinosa (following).
This is a simple script to display the uptime of MySQL.
-----------------
property newline : ASCII character 10
property tmpfile : "/tmp/execme"
set theShellScript to "#!/bin/sh" & newline & "/Library/MySQL/bin/mysql
-e STATUS | grep \"^Uptime\""
do shell script "echo " & quoted form of theShellScript & " > " &
tmpfile
do shell script "chmod +x " & tmpfile
set theTextResult to do shell script tmpfile
display dialog theTextResult
-----------------
Here's part of script I use to create a temporary text file.....
POSIX_date_folder is a variable created through the Finder and POSIX
Path
grep_pattern is just a string variable
-----------------
property newline : ASCII character 10
property tmpfile : "/tmp/execme"
set theShellScript to "#!/bin/sh" & newline & "usr/bin/find
/Users/Shared/" & POSIX_date_folder & " -print | egrep " & grep_pattern
& " > /tmp/filelist.txt"
do shell script "echo " & quoted form of theShellScript & " > " &
tmpfile
do shell script "chmod +x " & tmpfile
do shell script tmpfile
-----------------
Good Luck,
Ron Bishop
Macintosh Systems Administrator
The Kansas City Star
1728 Grand Boulevard
Kansas City, MO 64108
816/234-4943
Here's the original posting...
From: Chris Espinosa <email@hidden>
Date: Tue Aug 13, 2002 6:15:53 PM US/Central
To: email@hidden
Subject: do shell script with administrator privileges & shell commands
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.