简体   繁体   English

如何在 Matlab 的 system() 命令中包含双引号

[英]How to include double quote in Matlab's system() command

I need to pass filename as input to a powershell command which includes spaces.我需要将文件名作为输入传递给包含空格的 powershell 命令。 Hence I am hoping to include double quotes.因此,我希望包括双引号。

However, Matlab "eats" all double quotes in the input of system() when passing arguments to powershell.但是,当将 arguments 传递给 powershell 时,Matlab “吃掉”了system()输入中的所有双引号。

For example, note how the examples below all the the same output.例如,请注意下面的示例如何都相同 output。

>> system('powershell.exe echo a c','-echo')
a 
c 
ans =
     0
>> system('powershell.exe echo "a c"','-echo')
a 
c 
ans =
     0
>> system('powershell.exe echo ""a c""','-echo')
a 
c 
ans =
     0
>> system(['powershell.exe echo ',char(34),'a c',char(34)],'-echo')
a 
c 
ans =
     0

The actual output for echo "ac" in powershell is a c in a single line. powershell 中echo "ac"的实际 output 是单行中a c The change of line only occurs without double quotes.换行只发生在没有双引号的情况下。

Just for experiment, I also tried ""ac"" and the expected input is the same as change line, a, change line, c.只是为了实验,我还尝试了""ac"" ,预期的输入与更改线相同,a,更改线,c。 With the return, it seems that any and all double quotes are "eaten" alive by Matlab.随着返回,似乎所有双引号都被 Matlab “吃掉”了。

How do I bring the double quotes back when using system() ?使用system()时如何带回双引号?

It is PowerShell that is eating your double quotes:吃掉你的双引号的是PowerShell

  • You're passing a command (piece of PowerShell code) to the PowerShell CLI , via the -Command ( -c ) parameter (which is positionally implied in your case).您正在通过-Command ( -c ) 参数(在您的案例中隐含位置)将命令(一段 PowerShell 代码)传递给 PowerShell CLI

  • " characters that should be considered part of the command must be escaped as \" (sic) "应被视为命令一部分的字符必须转义为\" (原文如此)

    • The reason that unescaped " don't work is that PowerShell considers them to have syntactic function on the command line only - they are simply stripped after all arguments have been parsed; the resulting tokens are then joined with spaces, and the resulting string finally interpreted as PowerShell code.转义"不起作用的原因是 PowerShell 认为它们仅在命令行上具有语法 function - 在所有 arguments 已被解释后,它们被简单地剥离;并解析结果字符串,最终解释为作为 PowerShell 代码。
  • While using just \" in your command would fix the problem, it is advisable to also enclose the entire command being passed in "..." , because that prevents potentially unwanted whitespace normalization .虽然在您的命令中使用\"可以解决问题,但建议将传递的整个命令包含在"..."中,因为这样可以防止可能不需要的空白规范化

system('powershell.exe " echo \"a c\" "', '-echo')

Caveat :警告

  • Since MatLab's system() function executes the given command line via cmd.exe (which is inefficient in your case, since you don't need shell functionality), use of \" can break the invocation, due to how cmd.exe 's parses command lines. Since MatLab's system() function executes the given command line via cmd.exe (which is inefficient in your case, since you don't need shell functionality), use of \" can break the invocation, due to how cmd.exe 's解析命令行。

  • To avoid edge cases when cmd.exe is involved , enclose the overall command in "...." and escape pass-through " as follows:避免涉及cmd.exe时出现极端情况,请将整个命令括在"...."中并转义通过" ,如下所示:

    • Use "^"" (sic) when calling powershell.exe (the Windows PowerShell CLI)调用powershell.exe时使用"^"" (原文如此)( Windows PowerShell CLI)
    • Use "" when calling pwsh.exe (the PowerShell (Core) 7+ CLI).调用pwsh.exePowerShell (Core) 7+ CLI)时使用""
    • See this answer for more information.有关更多信息,请参阅此答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Windows上的os.system中的双引号转义 - double quote escaping in os.system on windows 如何将PowerShell的文字双引号传递给本机命令? - How do I pass a literal double quote from PowerShell to a native command? CMD / windows Batch:如何在命令参数中传递双引号和大于号? - CMD/windows Batch: how to pass a double quote and a larger than sign in a command argument? 在Windows下如何通过awk将单引号替换为双引号? - How to replace single quote to double quote by awk when under Windows? 如何将转义双引号传递给批处理文件 - How to pass escaped double quote to batch file 在Windows命令提示符中转义双引号副作用 - Escaped Double Quote Side Effect in Windows Command Prompt 由于双引号导致 Windows 上的 Golang exec.Command 错误 - Golang exec.Command error on Windows due to double quote 为什么Windows XP上Python的os.system()命令在开始运行时需要两个双引号? - Why does Python's os.system() command on Windows XP require two double quotes at the beginning to run? 如何在 Windows 上的“for /F”循环命令中引用空格 - How to quote spaces in the command of a “for /F” loop on Windows 如何在 curl.exe 的 -d 参数中发送双引号? - How to send double quote in -d parameter for curl.exe?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM