看起来这应该很简单,但我无法让它发挥作用。我有一个很长的shell命令,我想跨越多行,但正确地转发单/双引号给我带来麻烦。我搜索过SO并用Google搜索,但找不到解决方案。
原创,工作代码:
getVersion <- function() {
version <- shell("powershell -Command $path = 'C:/Windows/notepad.exe'; $versioninfo = [System.Diagnostics.FileVersionInfo]::GetVersionInfo($path); $itemproperties= get-childitem $path; [pscustomobject]@{'File version' = $versioninfo.FileVersion}", intern = TRUE)
version <- gsub(" ", "", version[4])
return(version)
}
不起作用:
getVersion <- function() {
version <- shell(paste0(\""powershell -Command $path = 'C:/Windows/notepad.exe'; $versioninfo = [System.Diagnostics.FileVersionInfo]::GetVersionInfo($path);\",
\"$itemproperties= get-childitem $path; [pscustomobject]@{'File version' = $versioninfo.FileVersion}"\"), intern = TRUE)
version <- gsub(" ", "", version[4])
return(version)
}
还试过这个:
getVersion <- function() {
version <- paste0('shell(\"powershell -Command $path = "C:/Windows/notepad.exe"; $versioninfo = [System.Diagnostics.FileVersionInfo]::GetVersionInfo($path);',
'$itemproperties= get-childitem $path; [pscustomobject]@{"File version" = $versioninfo.FileVersion}\", intern = TRUE)')
version <- gsub(" ", "", version[4])
return(version)
}
答案 0 :(得分:1)
你为什么想逃跑?你实际上在你需要通过的字符串中没有双引号,对吧?因此,您可以使用双引号字符串来执行包含单引号的paste()。或者我错过了什么?
例如,这会有用吗?
getVersion <- function() { version <- shell(paste("powershell -Command $path = 'C:/Windows/notepad.exe';",
"$versioninfo = [System.Diagnostics.FileVersionInfo]::GetVersionInfo($path);",
"$itemproperties= get-childitem $path;",
"[pscustomobject]@{'File version' = $versioninfo.FileVersion}"),
intern = TRUE)
version <- gsub(" ", "", version[4])
return(version) }