1

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?

mete
  • 1,188
  • 5
  • 16
  • 1
    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. – SquareOne Dec 15 '14 at 14:24
  • Works for me thanks. You should expand it to an answer – mete Dec 15 '14 at 14:45

1 Answers1

3

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.

SquareOne
  • 7,575
  • 1
  • 15
  • 34