1
$\begingroup$

I have problems running the bash code

mdfind -onlyin /some/folder my_string

This works on the terminal, but if I try

RunProcess[{"/usr/bin/mdfind", "-onlyin /some/folder", "my_string"}]

I get as an error

 Unknown option -onlyin /some/foder

While

RunProcess[{"/usr/bin/mdfind", "-onlyin", "/some/folder my_string"}]

gives as an error

/usr/bin/mdfind: no query specified

What is the correct way of handling this?

$\endgroup$
2
  • 1
    $\begingroup$ All the elements must be in separate items, this works for me : RunProcess[{"mdfind", "-onlyin", "/some/folder", "my_string"}] or RunProcess[{"mdfind", "-onlyin", "/some/folder", "my_string"}, "StandardOutput"] if you only need the standard output of the command, here the filenames. $\endgroup$
    – SquareOne
    Dec 15, 2014 at 14:24
  • $\begingroup$ Works for me thanks. You should expand it to an answer $\endgroup$
    – mete
    Dec 15, 2014 at 14:45

1 Answer 1

3
$\begingroup$

I actually found 3 ways to execute your command with RunProcess (on Unix like systems) :

Let's write your command as a string :

mycommand = "mdfind -onlyin /some/folder my_string";

but it could be any other command like :

mycommand = "ls -la";

Then these 3 inputs are equivalent :

RunProcess[StringSplit@mycommand]

RunProcess[{$SystemShell, "-c", mycommand}]

RunProcess[$SystemShell, All, mycommand <> "\nexit\n"]

In case you are just interested in the output not in the exit code nor error message, you just have to add/specify "StandardOutput" :

RunProcess[StringSplit@mycommand, "StandardOutput"]

RunProcess[{$SystemShell, "-c", mycommand}, "StandardOutput"]

RunProcess[$SystemShell, "StandardOutput", mycommand <> "\nexit\n"]

See also this and that posts for other useful approaches.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.