• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
stderr from 'do shell script': Not always in AS error or redirected as expected
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

stderr from 'do shell script': Not always in AS error or redirected as expected


  • Subject: stderr from 'do shell script': Not always in AS error or redirected as expected
  • From: Jerry Krinock <email@hidden>
  • Date: Mon, 16 Mar 2009 18:53:03 -0700

After reading TN2065 over many times, and studying about redirects in the bash shell, I've concluded that 'do shell script' does not always give stderr/stdout where expected. The demo below illustrates the problem when the command involves gcc (the "C" compiler).

In one case (Case 4), stderr is not redirected to stdout as it should be. In another case (Case 5), non-redirected stderr does not appear in Applescript's try/on error but simply disappears.

The problem does not seem to be in gcc or bash because if I redirect output in Terminal to files with commands like this:
gcc -c /Users/jk/source.c 2>myStdErr.txt 1>myStdOut.txt
or this:
gcc -c /Users/jk/badSource.c 1>myStdOut.txt 2>&1


then all behaves as expected.

If you've ever had trouble getting stdout and stderr, or think I'm wrong, run the following script...

Thanks,

Jerry Krinock


display dialog "This script illustrates unexpected behavior in returning the stdout and stderr of a 'do shell script' command. Run the script and see results in your system.log (Console.app) from Sender 'ShellBug'."


-- No Errors

set cmd to "echo MyEcho"
doShellScript(cmd, "Case 1.  No Errors without redirect", false)
doShellScript(cmd, "Case 2.  No Errors with redirect", true)

-- Error from gcc

set posixHome to POSIX path of (path to home folder)
set sourcePath to posixHome & "source.c"
set sourceCode to "This will give a short error"
writeTextToPosixPath(sourcePath, sourceCode, false)

set cmd to "gcc -c " & sourcePath
doShellScript(cmd, "Case 3.  Error from gcc without redirect", false)
doShellScript(cmd, "Case 4.  Error from gcc with redirect", true)

-- Warning from gcc

set sourceCode to "
#warning This is a warning
#include <stdio.h>
int main (int argc, const char * argv[]) {
    printf(\"Hello\\n\");
    return 0;
}"
writeTextToPosixPath(sourcePath, sourceCode, false)

set cmd to "gcc -c " & sourcePath
doShellScript(cmd, "Case 5.  gcc warning without redirect", false)
doShellScript(cmd, "Case 6.  gcc warning with redirect", true)

display dialog "Cases 1 and 2 are as expected: stdout is not redirected.

Case 3 is as expected: stderr is not redirected.

Case 4 is NOT as expected: stderr should have been redirected but it is not

Case 5 is NOT as expected: stderr did not appear in either script output nor error.

Case 6 is as expected: stderr was redirected to script output.

The problem does not seem to be in gcc or bash because if I redirect output in Terminal to files with commands like this:
gcc -c /Users/jk/source.c 2>myStdErr.txt 1>myStdOut.txt
or this:
gcc -c /Users/jk/badSource.c 1>myStdOut.txt 2>&1


then all behaves as expected.
"

on doShellScript(cmd, testName, doRedirectStderrToStdout)
    set aResult to "No result"
    set errorString to "No error string"
    set errorNumber to "No error number"
    if doRedirectStderrToStdout is true then
        set cmd to cmd & " 2>&1"
    end if
    try
        set aResult to do shell script cmd
    on error errStr number errNum
        set errorString to errStr
        set errorNumber to errNum
    end try
    syslogger("ShellBug", "****** " & testName & " ******")
    syslogger("ShellBug", "result: " & aResult)
    syslogger("ShellBug", "errorString: " & errorString)
    syslogger("ShellBug", "errorNumber: " & errorNumber)
    return aResult
end doShellScript

on writeTextToPosixPath(aPath, newText, doAppend)
-- filename must not end in ".log" or else a "file already open" error will result.
-- Maybe some system logging daemon takes over all .log files??
-- Uses File Read/Write suite of the Standard Additions scripting addition
if aPath begins with "~" then
set home_posix_path to POSIX path of (path to home folder)
set aPath to home_posix_path & characters 2 thru end of aPath
end if
set filePath to (POSIX file aPath as text)
set fileRef to open for access filePath with write permission
if doAppend is true then
write newText to fileRef starting at eof
else
-- Wipe out all existing text first
set eof of fileRef to 0
write newText to fileRef
end if
close access fileRef
end writeTextToPosixPath


(* Logs a message to the system log (which you can monitor in Console.app). The msg is tagged with a given sender. *)
on syslogger(sender, msg)
if (length of msg is greater than 1500) then
set msg to (text 1 thru 500 of msg) & " <snip> *** Sorry, Message > 1500 chars will not be reliably logged by BSD's 'logger'."
end if
set cmd to "logger -t " & quoted form of sender & " " & quoted form of msg
do shell script cmd
end syslogger



_______________________________________________ Do not post admin requests to the list. They will be ignored. AppleScript-Users mailing list (email@hidden) Help/Unsubscribe/Update your Subscription: Archives: http://lists.apple.com/archives/applescript-users

This email sent to email@hidden
  • Follow-Ups:
    • Re: stderr from 'do shell script': Not always in AS error or redirected as expected
      • From: Axel Luttgens <email@hidden>
  • Prev by Date: Re: Applescript newbie trying to do something easy
  • Next by Date: Re: AppleScript Language Guide: 404 Not Found
  • Previous by thread: Re: Applescript newbie trying to do something easy
  • Next by thread: Re: stderr from 'do shell script': Not always in AS error or redirected as expected
  • Index(es):
    • Date
    • Thread