Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

Re: Unable to grab STDERR from a backtick command

by almut (Canon)
on Mar 05, 2010 at 12:29 UTC ( #826954=note: print w/replies, xml ) Need Help??

in reply to Unable to grab STDERR from a backtick command

I think it depends on at what exact point the shell is doing the 2>&1 redirection.

I can't reproduce the problem with the command you've shown, i.e. here I do get

$ ./ CMD : >>echddo 'arghh'<< RETURN_CODE: >>32512<< CMD_RESULT : >>sh: echddo: not found <<

but when I remove one single quote ($cmd = "echddo arghh'"), so the shell command has a syntax error, I get like you

$ ./ sh: Syntax error: Unterminated quoted string CMD : >>echddo arghh'<< RETURN_CODE: >>512<< CMD_RESULT : >><<

The corresponding straces reveal that the redirection simply isn't being done in the latter case (note the missing dup2(1,2) ), presumably because my shell does the syntax checking before the 2>&1 redirection (which makes sense...), while in the other case, it does the redirection before starting to look for an executable 'echddo':

( cmd: echddo 'arghh' 2>&1 ) ... [pid 15950] rt_sigaction(SIGTERM, NULL, {SIG_DFL}, 8) = 0 [pid 15950] rt_sigaction(SIGTERM, {SIG_DFL}, NULL, 8) = 0 [pid 15950] fcntl(2, F_DUPFD, 10) = 10 [pid 15950] close(2) = 0 [pid 15950] fcntl(10, F_SETFD, FD_CLOEXEC) = 0 [pid 15950] dup2(1, 2) = 2 [pid 15950] stat("/home/almut/bin/echddo", 0x7fffb62454b0) = -1 ENOENT + (No such file or directory) [pid 15950] stat("/usr/local/sbin/echddo", 0x7fffb62454b0) = -1 ENOENT + (No such file or directory) [pid 15950] stat("/usr/local/bin/echddo", 0x7fffb62454b0) = -1 ENOENT +(No such file or directory) [pid 15950] stat("/usr/sbin/echddo", 0x7fffb62454b0) = -1 ENOENT (No s +uch file or directory) [pid 15950] stat("/usr/bin/echddo", 0x7fffb62454b0) = -1 ENOENT (No su +ch file or directory) [pid 15950] stat("/sbin/echddo", 0x7fffb62454b0) = -1 ENOENT (No such +file or directory) [pid 15950] stat("/bin/echddo", 0x7fffb62454b0) = -1 ENOENT (No such f +ile or directory) [pid 15950] write(2, "sh: ", 4) = 4 [pid 15950] write(2, "echddo: not found", 17) = 17 [pid 15950] write(2, "\n", 1) = 1 ... ( cmd: echddo arghh' 2>&1 ) ... [pid 15945] rt_sigaction(SIGTERM, NULL, {SIG_DFL}, 8) = 0 [pid 15945] rt_sigaction(SIGTERM, {SIG_DFL}, NULL, 8) = 0 [pid 15945] write(2, "sh: ", 4sh: ) = 4 [pid 15945] write(2, "Syntax error: Unterminated quote"..., 40Syntax e +rror: Unterminated quoted string) = 40 [pid 15945] write(2, "\n", 1 ...

What shell are you using?  (bash v3.2.39 here)

Your version seems to be trying to find the binary for 'echddo' before having set up the redirection...

Replies are listed 'Best First'.
Re^2: Unable to grab STDERR from a backtick command (2>&1)
by tye (Sage) on Mar 05, 2010 at 16:06 UTC

    A little-known (and perhaps undocumented) feature of Perl is that a trailing "2>&1" on a command will be handled by Perl itself (not the shell) if there is no other reason to use a shell (if there are no other shell metacharacters in the given command string). (At least that was true quite a while ago when I was reading some of the Perl source code and I don't expect such a feature to be removed.)

    So this problem would go away if the (useless) single quotes were removed from the example $cmd string. Yes, that "fix" likely won't work for the real problem.

    I find it unfortunate that there is no way to trigger this feature when passing a list to system.

    Update: Note that there are several modules to choose from that do "IPC" or "run commands" that can handle the "2>&1" for you. Or you can just handle it yourself using open (the 'fork' and 'dup' forms) and exec. Maybe I'll post an example of doing that in a reply, since I'd have to reread a few things to be able to write such.

    - tye        

Re^2: Unable to grab STDERR from a backtick command
by reasonablekeith (Deacon) on Mar 05, 2010 at 12:36 UTC
    GNU bash, version 2.05.0(1)-release (sparc-sun-solaris2.9)

    I'm guessing that's quite old then.

    I've also have another perl install on here, and am able to reproduce the error using that, so this doesn't look specific to the install I did.



    my name's not Keith, and I'm not reasonable.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://826954]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2022-09-30 10:24 GMT
Find Nodes?
    Voting Booth?
    I prefer my indexes to start at:

    Results (126 votes). Check out past polls.