Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
up vote 7 down vote accepted

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.

share|improve this answer
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). – Gordon Davisson May 18 '11 at 17:03
    
Thanks to you both – UrEl May 18 '11 at 17:28
    
@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

You must log in to answer this question.

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