14

I have a somewhat complex bash command which includes some pipes and an if-then-else clause, not to mention calling system program (such as grep) with multiple parameters.

Is there a way to create a plist that launchd will load and run this complex command directly? (As opposed to having the plist reference a bash script file containing the complex command)

Thanks.

UrEl
  • 841
  • 2
  • 11
  • 14

1 Answers1

17

Yes. When setting up the ProgramArguments element, simply use bash as the first argument, -c as the second argument, then [your commands] (remembering to replace any XML entities, such as >, & and so on) as the third, like so:

<key>ProgramArguments</key>
<array>
        <string>/bin/bash</string>
        <string>-c</string>
        <string>ls -1 | grep *.txt | echo &gt; allTextFiles</string>
</array>

Replace the third element of the array with your actual command.

Daniel Beck
  • 109,300
  • 14
  • 287
  • 334
Scott
  • 5,853
  • 1
  • 21
  • 33
  • 1
    I haven't tested this, but I'm pretty sure you don't want quotes around the command string, but that you do need to html-entity encode it (e.g. use `>` and `<` instead of `>` and `<` for redirections). – Gordon Davisson May 18 '11 at 17:03
  • @Gordon Davisson - you're certainly right about the quotes - they serve the same purpose as making it one element of the array. I'm pretty sure you're right about the XML entities, too. Good catch. I've edited my answer accordingly. – Scott May 18 '11 at 18:09