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.

1 Answer 1

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.

2
  • 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 &gt; and &lt; instead of > and < for redirections). May 18, 2011 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, 2011 at 18:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .