Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion Groups
General
GeneralPortable MacsHardwareNetworking
Applications
Mac ApplicationsEudoraFirefox / MozillaInternet ExplorerOutlook ExpressMS OfficeEntourageExcelPowerPointWordVirtual PCMedia PlayerOther MS Products
Programming
Mac ProgrammingCodeWarriorPerl
Country Specific
Australian Mac GroupUK Mac Group

Mac Forum / Programming / Mac Programming / January 2005



Tip: Looking for answers? Try searching our database.

Do Shell Script weirdness

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
iStrain - 25 Jan 2005 18:17 GMT
Anybody see this? I've got a bash script that runs just fine. No errors
- the $? status is checked everywhere. I've got a function that calls
oascript to speak a message if there's a nonzero status, and echo the
status value. Like I said, it all works fine from the shell.

Now if I run it from an AppleScript with do shell script
"/usr/local/bin/myscript" - that's all there is - the function says
that there's an error. However the little result log that comes up in
the Script Editor shows that the status is actually zero. The script
runs fine, the status is zero, but the message is spoken! I've changed
the script to fully verbose commands, and...nothing. It's fine.

checkStatus() {
if [ $? != 0 ]
then
   echo $?
   osascript -e 'say "bash script failed!"'
fi
}

What the dilly?
Signature

"It's beyond my ken... and my Barbie, and all of my action figures."

matt neuburg - 25 Jan 2005 19:40 GMT
> Anybody see this? I've got a bash script that runs just fine. No errors
> - the $? status is checked everywhere. I've got a function that calls
[quoted text clipped - 17 lines]
>
> What the dilly?

Works fine here. Perhaps you forgot the shebang line at the start of the
bash script? m.

Signature

matt neuburg, phd = matt@tidbits.com, http://www.tidbits.com/matt/
AppleScript: The Definitive Guide
http://www.amazon.com/exec/obidos/ASIN/0596005571/somethingsbymatt
Read TidBITS! It's free and smart. http://www.tidbits.com

iStrain - 26 Jan 2005 02:39 GMT
> Works fine here. Perhaps you forgot the shebang line at the start of the
> bash script? m.

Thanks matt. Nope, the bash script itself works just fine.
Signature

"It's beyond my ken... and my Barbie, and all of my action figures."

Dave Seaman - 25 Jan 2005 20:03 GMT
> Anybody see this? I've got a bash script that runs just fine. No errors
> - the $? status is checked everywhere. I've got a function that calls
> oascript to speak a message if there's a nonzero status, and echo the
> status value. Like I said, it all works fine from the shell.

> Now if I run it from an AppleScript with do shell script
> "/usr/local/bin/myscript" - that's all there is - the function says
> that there's an error. However the little result log that comes up in
> the Script Editor shows that the status is actually zero. The script
> runs fine, the status is zero, but the message is spoken! I've changed
> the script to fully verbose commands, and...nothing. It's fine.

> checkStatus() {
> if [ $? != 0 ]
[quoted text clipped - 3 lines]
> fi
> }

The $? variable represents the exit status of the last command.  That
means it is reset by each command in your script, including the
if statement on line 2.

If you want to examine the same $? value more than once, you need to save
it somewhere before it disappears.  For example:

    checkStatus() {
    rc = $?
    if [ $rc != 0 ]
    then
        echo $rc
        osascript -e 'say "bash script failed!"'
    fi
    }

Signature

Dave Seaman
Judge Yohn's mistakes revealed in Mumia Abu-Jamal ruling.
<http://www.commoncouragepress.com/index.cfm?action=book&bookid=228>

iStrain - 26 Jan 2005 03:00 GMT
> The $? variable represents the exit status of the last command.  That
> means it is reset by each command in your script, including the
[quoted text clipped - 11 lines]
>     fi
>     }

Oh yeah, I know, thanks. I was just pasting in a snippet to show the
general idea. I actually do what you wrote, and echo the result every
time thru (I'm echoing everything to try and figure this one out).  
It's really bizarre-  everytime I execute thru the terminal it's fine,
and everytime thru applescript it's failing with 127.

Turns out I was stupid before. I was finally able to verify that it's
_not_ completely working correctly under applescript; it seemed to be
working before, simply because I was running the tests too closely
together in time. So, the applescript is definitely failing!

The err 127 is usually $PATH related, isn't it? I wonder if something's
differernt about the way the shell script is being executed. The script
is failing on a mysql command, which in this case is in
/Library/MySQL/bin/. Is it possible that a script executed form
applescript goes stupid with this path?
Signature

"It's beyond my ken... and my Barbie, and all of my action figures."

Dave Seaman - 26 Jan 2005 04:14 GMT
> The err 127 is usually $PATH related, isn't it? I wonder if something's
> differernt about the way the shell script is being executed. The script
> is failing on a mysql command, which in this case is in
> /Library/MySQL/bin/. Is it possible that a script executed form
> applescript goes stupid with this path?

If you are setting your PATH in your shell startup files, then this works
fine in your shell sessions but does not apply to GUI apps such as
AppleScript.  The prescribed way to set your environment for GUI apps is
to create a ~/.MacOSX directory and to store an environment.plist file
there in XML format.  Here is what mine looks like.  It sets DISPLAY to
:0.0 and sets my PATH as I normally use it.  I'm sure you'll get the
idea:

[gemini:dseaman] $ cat .MacOSX/environment.plist
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN"
"http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
       <key>DISPLAY</key>
       <string>:0.0</string>
       <key>PATH</key>
       <string>/Users/dseaman/local/bin:/Users/dseaman/local/bin/Darwin:/usr/local/bin:/usr/X11R6/bin:/Developer/Tools:/Applications/Absoft/bin:/sw/bin:/sw/sbin:/bin:/sbin:/usr/bin:/usr/sbin:.</string>
</dict>
</plist>

I can tell you that it makes a difference in some cases I have tried.  I
haven't particularly tried it with AppleScript, which I have not found
much use for, but I suspect that this approach will probably work.

That's if you decide you still want to use AppleScript after reading the
man page for say(1), as suggested elsewhere.

Signature

Dave Seaman
Judge Yohn's mistakes revealed in Mumia Abu-Jamal ruling.
<http://www.commoncouragepress.com/index.cfm?action=book&bookid=228>

iStrain - 30 Jan 2005 13:42 GMT
> If you are setting your PATH in your shell startup files, then this works
> fine in your shell sessions but does not apply to GUI apps such as
[quoted text clipped - 15 lines]
>        
> <string>/Users/dseaman/local/bin:/Users/dseaman/local/bin/Darwin:/usr/local/bin:/usr/X11R6/bin:/Developer/Tools:/Applications/Absoft/bin:/sw/bin:/sw/sbin:/bin:/sbin:/usr/bin:/usr/sbin:.</string>
</dict>
</plist>

I

> </dict>
> </plist>
>
> I can tell you that it makes a difference in some cases I have tried.  I
> haven't particularly tried it with AppleScript, which I have not found
> much use for, but I suspect that this approach will probably work.

Thanks  that's very cool.
Signature

"It's beyond my ken... and my Barbie, and all of my action figures."

Jeffrey Jones - 26 Jan 2005 03:06 GMT
>  checkStatus() {
>  rc = $?
[quoted text clipped - 4 lines]
>  fi
>  }

I think Dave Seaman already answered your question, but ...  Did you
know you don't neeed osascript for this?  Mac OS X has a "say" command
(man say):

say 'bash script failed!'
iStrain - 30 Jan 2005 13:41 GMT
>> checkStatus() {
>> rc = $?
[quoted text clipped - 10 lines]
>
> say 'bash script failed!'

No, I didn't know that! thanks.
Signature

"It's beyond my ken... and my Barbie, and all of my action figures."

iStrain - 26 Jan 2005 03:13 GMT
I think I just answered my own question. The (prebuilt) package I used
for MySQL installs the binaries in /Library/MySQL/bin/, with a symlink
/usr/local/mysql - with the binaries in /usr/local/mysql/bin/,
following the link. I think that the applescript doesn't know how to
follow the link down, and expects the stuff to be in /usr/local/bin/,
not /usr/local/mysql/bin. Whaddaya think?

Signature

"It's beyond my ken... and my Barbie, and all of my action figures."

iStrain - 26 Jan 2005 03:27 GMT
Okay, I'm going to eat a big "RTFM" today.

<http://developer.apple.com/technotes/tn2002/tn2065.html>

Signature

"It's beyond my ken... and my Barbie, and all of my action figures."

macshaggy - 26 Jan 2005 15:05 GMT
I've had the same problem until I put the command into a tell..end tell

tell application "Terminal"
do shell script "/usr/local/bin/myscript"
end tell

try that - the problem your having is not with your script it's with
the do shell script command in AppleScript thats having problems.
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.