Forum: CFEngine Help
Subject: Odd behaviour of splitstring
Author: neilhwatson
Link to topic: https://cfengine.com/forum/read.php?3,25023,25023#msg-25023

My goal is to make a string from an external command, convert the string into a 
list and then grep only the list items I want.


bundle agent iptables_save(f){

        vars:

                "raw" string => execresult( "/bin/cat /root/inputs/now", 
"noshell");

                "lines" slist => splitstring( "${raw}", "\n", "100000");

                "no_comments" slist => grep( ".*?[^#]*", "lines" );

     reports:

                cfengine::

                        "raw: ${raw}";
                        "lines: ${lines}";
                        "no_comments: ${no_comments}";

}


In the end the raw string will come from a command.  The cat is just for 
testing.  Now let's run the bundle.

root@sol:~/inputs# cf-agent -IKf ./edittest.cf 
R: raw: # Generated by iptables-save v1.4.8 on Fri Feb 24 17:32:10 2012
*raw
:PREROUTING ACCEPT [377:34237]
:OUTPUT ACCEPT [159:45044]
COMMIT
# Completed on Fri Feb 24 17:32:10 2012
# Generated by iptables-save v1.4.8 on Fri Feb 24 17:32:10 2012
*nat
:PREROUTING ACCEPT [14:982]
:POSTROUTING ACCEPT [15:1058]
:OUTPUT ACCEPT [1:76]
:br0_masq - [0:0]
-A POSTROUTING -o br0 -j br0_masq 
-A br0_masq -s 192.168.122.0/24 -j SNAT --to-source 46.21.99.2 
COMMIT
R: lines: # Generated by iptables-save v1.4.8 on Fri Feb 24 17:32:10 2012
R: lines: *raw
R: lines: :PREROUTING ACCEPT [377:34237]
R: lines: :OUTPUT ACCEPT [159:45044]
R: lines: COMMIT
R: lines: # Completed on Fri Feb 24 17:32:10 2012
R: lines: *nat
R: lines: :PREROUTING ACCEPT [14:982]
R: lines: :POSTROUTING ACCEPT [15:1058]
R: lines: :OUTPUT ACCEPT [1:76]
R: lines: :br0_masq - [0:0]
R: lines: -A POSTROUTING -o br0 -j br0_masq
R: lines: -A br0_masq -s 192.168.122.0/24 -j SNAT --to-source 46.21.99.2
R: no_comments: *raw
R: no_comments: :PREROUTING ACCEPT [377:34237]
R: no_comments: :OUTPUT ACCEPT [159:45044]
R: no_comments: COMMIT
R: no_comments: *nat
R: no_comments: :PREROUTING ACCEPT [14:982]
R: no_comments: :POSTROUTING ACCEPT [15:1058]
R: no_comments: :OUTPUT ACCEPT [1:76]
R: no_comments: :br0_masq - [0:0]
R: no_comments: -A POSTROUTING -o br0 -j br0_masq
R: no_comments: -A br0_masq -s 192.168.122.0/24 -j SNAT --to-source 46.21.99.2
R: Files are different


Notice the last line 'COMMIT' in the raw string is lost after the split.   
Where did it go?

_______________________________________________
Help-cfengine mailing list
Help-cfengine@cfengine.org
https://cfengine.org/mailman/listinfo/help-cfengine

Reply via email to