[R & Unix ..] system("test",...) gives funny results...

Martin Maechler Martin Maechler <maechler@stat.math.ethz.ch>
Thu, 10 Dec 1998 14:57:58 +0100


[in an attempt to provide 

	  system.test <- 
		function(...) { system(paste("test", ...)) == 0 }
and then 
	  file.exists <- 
		function(file){sapply(file, function(f)system.test("-e", f))} 
 
  (which would provide S-plus [>= 4.x] compatibility)
]

Look at this -- ``the horror'' to me :

> (system("test -e /tmp"))
sh: test: argument expected
[1] 256
> system("type test")
test is a shell builtin
> system("echo $SHELL")
/usr/local/bin/tcsh

> (system("test -d /tmp")) # correct: it IS a directory
[1] 0
> (system("test -f /tmp")) # correct: it is NOT a plain file
[1] 256
> (system("/usr/local/bin/test -e /tmp"))  ## this is GNU test from sh-utils
[1] 0
> (system("/usr/bin/test -e /tmp")) ##- Solaris 2.5.x 
[1] 0
> (system("test -e /tmp"))
sh: test: argument expected
[1] 256

However, there doesn't seem to be a problem 
in the shell [/usr/local/bin/tcsh] :

    sophie{maechler}637> test -e /tmp; echo "$?"
    0
    sophie{maechler}638> test -e /tp; echo "$?"
    1

-----------------

Similar funny behavior  (in tcsh; $version gives the tcsh version)

In the shell

   sophie{maechler}639> echo $version
   tcsh 6.04.00 (Cornell) 93/07/03 (sun4) options 8b,nls,dl,al

In R :

   > (system("echo $version"))

   [1] 0

   > {system("echo $version > /tmp/qq"); scan("/tmp/qq",what="")}
   Read 0 items
   character(0)
   >

----------
One way to get an R internal "test" 
	-- even working in the Win/Mac version of R ?! --- 
would be to incorporate GNU sh-utils "test"
(standalone) and provide something like

	test <- function(...) .Internal(test(...))

However, this probably should have to be done by someone who understands
more than me about this kind of C codes.

----------
Comments, suggestions ?

Martin Maechler <maechler@stat.math.ethz.ch>	http://stat.ethz.ch/~maechler/
Seminar fuer Statistik, ETH-Zentrum SOL G1;	Sonneggstr.33
ETH (Federal Inst. Technology)	8092 Zurich	SWITZERLAND
phone: x-41-1-632-3408		fax: ...-1086			<><
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-devel-request@stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._