I am trying to run some Perl from R using system : simply assigning the string (provided in R) to a variable and repeating it. (a system call is made in /bin/sh )
echo <- function (string) { cmd <- paste(shQuote(Sys.which('perl')), '-e', shQuote(sprintf("$str=%s; print $str", shQuote(string)))) message(cmd) system(cmd) }
However, if I try to echo backslash (or even any line ending with a backslash), I get an error message:
> echo('\\') '/usr/bin/perl' -e "\$str='\\'; print \$str" Can't find string terminator "'" anywhere before EOF at -e line 1.
(Note: the backslash in front of $ fine, as it protects /bin/sh from thinking $str is a shell variable).
The error is that Perl interprets the last \' as an inline quote mark inside $str , and not an escape backslash. In fact, to get perl for the backslash echo, I need to do
> echo('\\\\') '/usr/bin/perl' -e "\$str='\\\\'; print \$str" \
That is, I need to avoid the backslash for Perl (in addition to the fact that I avoid them in R / bash).
How can I ensure in echo that the line the user enters is the line that is being printed? that is, the only shielding level that is needed is at level R?
i.e. is there any perlQuote function similar to shQuote ? Should I just avoid all the backslashes in my echo function? Are there any other characters that I need to avoid?
source share