shEncode: Quote Strings for Use in OS Shells

shEncodeR Documentation

Quote Strings for Use in OS Shells

Description

Quote a string to be passed to an operating system shell.

Usage

     shEncode(string, type = NULL, unix.type = NULL, windows.type = "Rscript")

commandEncode(string, type = NULL, unix.type = NULL, windows.type = "Rscript")

 commandQuote(string, type = NULL, unix.type = NULL, windows.type = "Rscript")

Arguments

string

a character vector, usually of length one.

type

character string: the type of quoting. Partial matching is supported.

unix.type

character string: the type of quoting under Unix-alikes when type is NULL.

windows.type

character string: the type of quoting in Windows when type is NULL.

Details

The choices for quoting are "sh", "perl", "python", "R", "R CMD", "Rcmd", "Rgui", "Rscript", and "Rterm". The default for quoting under Unix-alikes is "sh", while in Windows it is "Rscript".

From my testing with shQuote, it seems like it fails in a few edge cases that shEncode can handle correctly.

Those few cases that shQuote handles incorrectly are:

  • under Unix-alikeswith type = "csh" and string contains a dollar sign or grave accent and string has a trailing single quote, string loses its trailing single quote.

  • in Windowswith type = "cmd" and string contains a double quote preceded by at least one backslash, the preceding backslashes aren't escaped. Also, when string contains a set of trailing backslashes, they aren't escaped.

Value

character vector, the same length as string, with the attributes of string (after possible coercion to character), excluding class.

See Also

Rscript, python

Examples

fun <- function(string) {
    cat(c(
        "string  ", string                                        , "\n",
        "sh      ", essentials::shEncode(string, type = "sh"     ), "\n",
        "Rscript ", essentials::shEncode(string, type = "Rscript"), "\n",
        "R       ", essentials::shEncode(string, type = "R"      ), "\n"
    ), sep = "")
}

fun("abc$def`gh`i\\j")

fun("testing \\\"this\\\"")

fun("\"testing\" $this$ 'out'")


## Not run: 
essentials:::.system(paste(c(
    "perl",
    "-e",
    essentials::shEncode(r"{print "test \"this\" out\n";}",
        windows.type = "perl")
), collapse = " "))

## End(Not run)


## Not run: 
essentials:::.system(paste(c(
    "python",
    "-c",
    essentials::shEncode(r"{print("test \"this\" out")}",
        windows.type = "python")
), collapse = " "))

## End(Not run)

ArcadeAntics/essentials documentation built on Nov. 7, 2024, 4:33 p.m.