简体   繁体   English

从C#程序传递Shell命令,引号消失了

[英]Passing Shell Commands from a C# Program, Quotation Marks Disappearing

I am trying to execute a shell command on my Pi running on Raspbian but the quotation marks seem to disappear. 我正在尝试在运行Raspbian的Pi上执行shell命令,但是引号似乎消失了。

string quote = "\"";
string argument = "-vf" + " -hf" + " -o" + @" /home/pi/Desktop/camera/" + "$(date +" + quote + "%d%m%Y_%H%M-%S" + quote + ").jpg";
Process.Start("/usr/bin/raspistill", argument);

Here is the script I'm trying to execute: 这是我要执行的脚本:

sudo raspistill -vf -hf -o /home/pi/Desktop/camera/$(date +"%d%m%Y_%H%M-%S").jpg

And here is the error I'm getting: 这是我得到的错误:

Invalid command line option (+%d%m%Y_%H%M-%S).jpg) 无效的命令行选项(+%d%m%Y_%H%M-%S).jpg)

As you can see, the quotation marks seem to have disappeared. 如您所见,引号似乎消失了。

Any ideas? 有任何想法吗?

try replacing "$(date +" + quote + "%d%m%Y_%H%M-%S" + quote + ") 尝试替换“ $(date +” +引用+“%d%m%Y_%H%M-%S” +引用+“)

with: @"$(date+""%d%m%Y_%H%M-%S"")" 与:@“ $(date +”“%d%m%Y_%H%M-%S”“)”

To escape the double quote character: " 要转义双引号字符:

You actually have to double double quotes like so: "" 实际上,您必须像这样将双引号加倍:“”

Here is the DotNetFiddle I used to test this 是我用来测试的DotNetFiddle

It is not directly possible like this - $( ) is a shell expression and you do not invoke a shell but just only the raspistill command. 不可能像这样直接发生-$()是shell表达式,您不调用shell,而仅调用raspistill命令。 There seems to be not "quoting" problems that I see (see comments) 我看到的似乎没有“引用”问题(请参阅评论)

One way: Format the date string inside C# (I'm no expert but should be possible) and pass that formatted string directly 一种方法:在C#中格式化日期字符串(我不是专家,但应该可以)并直接传递该格式化的字符串

 ... +" -o" + " /home/pi/Desktop/camera/" + formateddate + ".jpg";

Other way: Try to invoke /bin/sh and pass 其他方式:尝试调用/ bin / sh并传递

"-c " + quote + "/usr/bin/raspistill " + argument + quote

as parameters: 作为参数:

string quote = "\"";
string argument = "-vf" + " -hf" + " -o" + 
" /home/pi/Desktop/camera/$(date +%d%m%Y_%H%M-%S).jpg";
Process.Start("/bin/sh", "-c " + quote + 
"/usr/bin/raspistill " +argument + quote);

However, I'm no C# expert and the way to pass it to ProcessStart may have to be fiddled around with. 但是,我不是C#专家,因此可能必须弄弄将其传递给ProcessStart的方法。 Also not sure where to place the sudo. 也不确定在哪里放置sudo。

Would you try and let uns know? 您会尝试让我们知道吗?

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM