not able to run Linux command through puppet

132 views
Skip to first unread message

Spriya

unread,
Jan 5, 2015, 7:33:09 PM1/5/15
to puppet...@googlegroups.com
Hi,

I am trying to run linux command through puppet. when i run the same command in the command line it is working fine. But when i placed it in the puppet it is throwing me an error.

Here is the code:

Facter.add('unknownjava') do
      setcode do
        %x(`/usr/bin/diff <(/bin/sort /home/weblogic/javafoundmodified.txt) <(/bin/sort /home/weblogic/authorizedjava.txt) > /home/weblogic/unknownjava.txt | /bin/sed -i 's/^.//' /home/weblogic/unknownjava.txt`)
      end
    end


Here is the  error:
sh: command substitution: line 0: syntax error near unexpected token `('
sh: command substitution: line 0: `/usr/bin/diff <("/bin/sort /home/weblogic/javafoundmodified.txt") <("/bin/sort /home/weblogic/authorizedjava.txt") > /home/weblogic/unknownjava.txt | /bin/sed -i 's/^.//' /home/weblogic/unknownjava.txt'
sh: command substitution: line 0: syntax error near unexpected token `('



Gareth Rushgrove

unread,
Jan 6, 2015, 8:42:59 AM1/6/15
to puppet...@googlegroups.com
I'm guessing here a little but I think this is because the command is
executing under sh, and when you run in manually it's executing under
bash.

So try something like:

(`bash -c /usr/bin/diff ....

Gareth

>
>
> --
> You received this message because you are subscribed to the Google Groups
> "Puppet Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to puppet-users...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/puppet-users/8b32a0d5-f06a-4598-9f72-b5a3aca4a610%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.



--
Gareth Rushgrove
@garethr

devopsweekly.com
morethanseven.net
garethrushgrove.com

Spriya

unread,
Jan 6, 2015, 2:10:21 PM1/6/15
to puppet...@googlegroups.com

Same error is coming up. even though i changed to 'bash -c /usr/bin/diff ...

Trevor Vaughan

unread,
Jan 6, 2015, 2:55:23 PM1/6/15
to puppet...@googlegroups.com
Looks like your Ruby syntax is incorrect.

Try (untested):
%x{/usr/bin/diff <(/bin/sort /home/weblogic/javafoundmodified.txt) <(/bin/sort /home/weblogic/authorizedjava.txt) > /home/weblogic/unknownjava.txt | /bin/sed -i 's/^.//' /home/weblogic/unknownjava.txt}

You might want to throw that in IRB before putting it in Facter directly just to make sure it does what you expect.

Trevor

--
You received this message because you are subscribed to the Google Groups "Puppet Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to puppet-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/puppet-users/8b32a0d5-f06a-4598-9f72-b5a3aca4a610%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Trevor Vaughan
Vice President, Onyx Point, Inc
(410) 541-6699
tvau...@onyxpoint.com

-- This account not approved for unencrypted proprietary information --

jcbollinger

unread,
Jan 6, 2015, 3:00:16 PM1/6/15
to puppet...@googlegroups.com


On Monday, January 5, 2015 1:33:09 PM UTC-6, Spriya wrote:

%x(`/usr/bin/diff <(/bin/sort /home/weblogic/javafoundmodified.txt) <(/bin/sort /home/weblogic/authorizedjava.txt) > /home/weblogic/unknownjava.txt | /bin/sed -i 's/^.//' /home/weblogic/unknownjava.txt`)

[...]
 
Here is the  error:
sh: command substitution: line 0: syntax error near unexpected token `('
sh: command substitution: line 0: `/usr/bin/diff <("/bin/sort /home/weblogic/javafoundmodified.txt") <("/bin/sort /home/weblogic/authorizedjava.txt") > /home/weblogic/unknownjava.txt | /bin/sed -i 's/^.//' /home/weblogic/unknownjava.txt'
sh: command substitution: line 0: syntax error near unexpected token `('



It looks like you have multiple wierdnesses going on there.

In the first place, you are nesting a backtick-quoted command execution inside a %x{ ... } block.  That will run the inner command, then (on success) run its output as a command.  I don't suppose that's what you want.

In the second place, you have a quoting problem.  Garet is onto something when he observes that the error is flagging a construct recognized by bash but not by the traditional Bourne shell.  His suggestion is probably right, except that he neglects to mention that the whole body of the command needs to be quoted as a single argument following -c.

In the third place, your command has odd redirections in it: you redirect the diff output to a file, but you also pipe it to sed.  There's only one standard output; you cannot redirect it to two different termini.

In the fourth place, you are both piping input into sed, and giving it a file name to process, and moreover, you are specifying in-place editing (-i).  This combination should edit the named file, ignoring standard input and producing nothing on standard output.  That's ok in itself, but it won't give you anything for your fact value.

Supposing that it is important to record the results of the command in a file, as the command seems intended to do, it looks like you want something like this:

Facter.add('unknownjava') do
  setcode
do

 
%x("/bin/bash -c '/usr/bin/diff <(/bin/sort /home/weblogic/javafoundmodified.txt) <(/bin/sort /home/weblogic/authorizedjava.txt) | /bin/sed \"s/^.//\" /home/weblogic/unknownjava.txt | /usr/bin/tee /home/weblogic/unknownjava.txt'")
 
end
 
end


John

jcbollinger

unread,
Jan 6, 2015, 3:02:16 PM1/6/15
to puppet...@googlegroups.com


Sorry, I meant:
%x{/bin/bash -c '/usr/bin/diff <(/bin/sort /home/weblogic/javafoundmodified.txt) <(/bin/sort /home/weblogic/authorizedjava.txt) | /bin/sed "s/^.//" /home/weblogic/unknownjava.txt | /usr/bin/tee /home/weblogic/unknownjava.txt'}


John

jcbollinger

unread,
Jan 6, 2015, 3:04:58 PM1/6/15
to puppet...@googlegroups.com

Errr... this:

%x{/bin/bash -c '/usr/bin/diff <(/bin/sort /home/weblogic/javafoundmodified.txt) <(/bin/sort /home/weblogic/authorizedjava.txt) | /bin/sed "s/^.//" | /usr/bin/tee /home/weblogic/unknownjava.txt'}



I mean it this time.


John

Felix Frank

unread,
Jan 6, 2015, 3:26:54 PM1/6/15
to puppet...@googlegroups.com
How about avoiding the awful bashisms altogether?

cat /home/weblogic/{javafoundmodified,authorizedjava}.txt
| sort
| uniq -u
| tee ...

That should make it easier on your Ruby.

Spriya

unread,
Jan 6, 2015, 3:33:11 PM1/6/15
to puppet...@googlegroups.com
Thank you john. This is working for me.

jcbollinger

unread,
Jan 7, 2015, 1:58:43 PM1/7/15
to puppet...@googlegroups.com


Nice one.  That probably does the job, but do note that it is not an exact equivalent -- it doesn't do the same thing if one file contains duplicate lines, and the other doesn't have any matching one.  Supposing it is acceptable to assume that situation will not arise, yours is cleaner, and probably doesn't require being wrapped in bash -c.


John

Reply all
Reply to author
Forward
0 new messages