From org.r-project.r-devel at pooryorick.com  Mon Sep 22 12:18:16 2008
From: org.r-project.r-devel at pooryorick.com (=?utf-8?Q?Nathan=20Coulter?=)
Date: Mon, 22 Sep 2008 10:18:16 +0000
Subject: [Rd] =?utf-8?q?handling_spaces_in_R_invocation_scripts?=
Message-ID: <20080922101816.12085.qmail@s461.sureserver.com>

On posix systems, invoking R with arguments containing shell word delimiters
(like space) doesn't currently work:

    bash-2.05b$ ./R -f "/path/to/script with spaces.R" 
    Fatal error: cannot open file '/path/to/script': No such file or directory

The attached patch, built against the devel snapshot of 2008-09-20, attempts to
fix this problem.  In R.sh.in and BATCH, it uses the only array available in a
portable script, positional parameters, to collect arguments, and does away
with $args, avoiding the quoting issues in the exec line which are currently
problematic.  The test criteria in the argument loop is replaced with an
explicit loop through all incoming positional parameters, eventually shifting
them all out and replacing them with modified parameters.  This handles shell
delimiters and blank positional arguments more gracefully.  Empty-string
positional arguments might not normally  be considered useful, but one never
knows.  

In BATCH, there isn't currently a realistic need to be absolutely careful about
passing shell parameters straight through, spaces and all, because the only
place one foresees spaces being encountered is in an --args argument, which the
current R CMD BATCH syntax hardly encourages.  But for consistency in the
source code, and to open the discussion about possibly changing the syntax of R
CMD BATCH to make --args easier to use, I changed the loop handling there as
well.  I'd prefer to see outfile specified with a "-o" option, instead of as a
possible final positional argument.  If there wasn't an optional argument after
infile, the semantics of "--" could be changed to mean the end of options to R
CMD BATCH, with all further arguments save the final one passed into the R
program (example below).

One implication of getting arg-passing right in the shell is that a workaround
people have been employing will behave differently.  Currently, the following
syntax gets some arguments into R in batch mode:

    ./R-devel CMD BATCH '--args arg1 arg2 arg3' "/path/to/script with spaces.R"

With the patch, each argument would need to be passed separately.  The
advantage is that shell word delimiters could occur:

    ./R-devel CMD BATCH --args "-a=argument 1" -arg2 -arg3 "/path/to/script with spaces.R"

Perhaps, though, this syntax would be better:

    ./R-devel CMD BATCH -o outfile.Rout -- --args "a=argument 1" arg2 arg3 "/path/to/script with spaces.R"

The patch also does away with extraneous curly brackets around variables.  This
probably trounces someone's shell-scripting style, but functionally, they're
useless, and visually, they don't accomplish anything that a syntax-colorizing
editor won't already make obvious.


-- 
Nathan Coulter




[Rd] =?utf-8?q?handling_spaces_in_R_invocation_scripts?=
=?utf-8?Q?Nathan=20Coulter?=