Search code examples
c#shellraspberry-piraspbian

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.

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)

As you can see, the quotation marks seem to have disappeared.

Any ideas?


Solution

  • It is not directly possible like this - $( ) is a shell expression and you do not invoke a shell but just only the raspistill command. 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

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

    Other way: Try to invoke /bin/sh and pass

    "-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. Also not sure where to place the sudo.

    Would you try and let uns know?