Skip to content

How to check if a pipe was successfully opened or not?

2 messages · Henrik Bengtsson, Brian Ripley

#
Is there a way to detect if the opening of a connection to a pipe was
successful or not? Here are two examples

 # Works
 > con <- pipe("ls")
 > res <- open(con, open="r")
 > print(res)
 NULL

 # Does not work
 > con <- pipe("unknown_command")
 > res <- open(con, open="r")
 > 'unknown_command' is not recognized as an internal or external
command,
 operable program or batch file.
 > print(res)
 NULL

Can I make my script recognize/detect that the latter failed? try() will
not catch the error. The error message is not written to stdout so
sink() won't "catch" it either. Does anyone know of a (cross-platform)
way to test if "unknown_command" exists or not on the current system
before calling pipe()/open()? 

I'm running R v1.7.0 on WinXP.

Thanks

Henrik Bengtsson

Dept. of Mathematical Statistics @ Centre for Mathematical Sciences
Lund Institute of Technology/Lund University, Sweden 
(Sweden +2h UTC, Melbourne +10 UTC, Calif. -7h UTC)
+46 708 909208 (cell), +46 46 320 820 (home), 
+1 (508) 464 6644 (global fax),
+46 46 2229611 (off), +46 46 2224623 (dept. fax)
h b @ m a t h s . l t h . s e, http://www.maths.lth.se/~hb/
#
On Fri, 30 May 2003, Henrik Bengtsson wrote:

            
open() always returns NULL.
The C code called by open() has

    fp = popen(con->description, mode);
    if(!fp) {
	warning("cannot open cmd `%s'", con->description);
	return FALSE;
    }

so presumably your system's popen is returning a FILE stream even though 
the command cannot be opened.  Not much we can do about that.  Solaris 
says

    The popen() function returns a null pointer if files or
    processes cannot be created.

but I think the problem is that the `process' is that launching the shell, 
not that of the command.  I am not using Windows, so cannot check there.