Regex in slist

32 views
Skip to first unread message

Christian Linden

unread,
Aug 24, 2016, 6:50:10 AM8/24/16
to help-cfengine
Hi,

is there no way to use regex in an slist like:
 vars:
    "alphabet" slist => { "[b-z]" };

Shortest I found is:
 vars:
    "alphabet" slist => string_split("b:c:d:e:f:g:h:i:j:k:l:m:n:o:p", ":", "25");

?

Thanks,
Chris

Aleksey Tsalolikhin

unread,
Aug 24, 2016, 8:28:02 AM8/24/16
to Christian Linden, help-cfengine
On Wed, Aug 24, 2016 at 3:50 AM, Christian Linden <lindo...@gmail.com> wrote:
Hi,

is there no way to use regex in an slist like:
 vars:
    "alphabet" slist => { "[b-z]" };

In short, no. A regular expression describes a pattern in text, and then you can check if your text matches the pattern.
 A list is a collection of one or more typed elements, and a name for that collection.  They are different.

Best,
-at

-- 
Need CFEngine training?  Email trai...@verticalsysadmin.com

Christian Linden

unread,
Aug 24, 2016, 5:08:52 PM8/24/16
to Aleksey Tsalolikhin, help-cfengine
hmm… in theory [b-z] could work if the expressions were supported at that point.
At least I’m pretty sure =)

c

Sean Johnson

unread,
Aug 24, 2016, 7:54:41 PM8/24/16
to help-cfengine
The regular expression has to filter some other piece of data, ostensibly the character set a->z, of which you only want b->p. The a->z character set would need to first exist, perhaps as a string variable, and then your regular expression would consume that string to break out into character elements of b->p. To my eyes, that doesn’t provide any benefit over using something like the string_split function.

sean
> --
> You received this message because you are subscribed to the Google Groups "help-cfengine" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to help-cfengin...@googlegroups.com.
> To post to this group, send email to help-c...@googlegroups.com.
> Visit this group at https://groups.google.com/group/help-cfengine.
> For more options, visit https://groups.google.com/d/optout.
signature.asc

Christian Linden

unread,
Aug 25, 2016, 5:20:28 AM8/25/16
to Sean Johnson, help-cfengine
That makes sense, Sean, thank you.
Error in reasoning, got it.

c
signature.asc

Ted Zlatanov

unread,
Aug 25, 2016, 3:11:37 PM8/25/16
to Sean Johnson, Christian Linden, help-cfengine
On Wed, 24 Aug 2016 18:54:36 -0500 "Sean Johnson" <se...@ttys0.net> wrote:

SJ> The regular expression has to filter some other piece of data, ostensibly the
SJ> character set a->z, of which you only want b->p. The a->z character set would
SJ> need to first exist, perhaps as a string variable, and then your regular
SJ> expression would consume that string to break out into character elements of
SJ> b-> p. To my eyes, that doesn’t provide any benefit over using something like the
SJ> string_split function.

expandrange() does almost exactly what Christian wants, but with
numbers, so there's some precedent in CFEngine for easier specification
of a long slist. expandrange() could be hacked to do letters in addition
to numbers... but I don't have a valid use case, and as far as I know it
hasn't ever been requested. (This request, I think, is better solved by
my example below.)

On Thu, 25 Aug 2016 06:04:26 -0700 (PDT) Christian Linden <lindo...@gmail.com> wrote:

CL> I want to mount a /dev/sd$(alphabet) as soon as it's available.

Fortunately, findfiles() supports shell range expressions and wildcards:

#+begin_src cfengine3
bundle agent main
{
vars:
"found_alphabet" slist => findfiles("/dev/sd[b-p]");
"found_letters" slist => findfiles("/dev/sd?");

reports:
"$(found_alphabet) is found on the system by CFEngine.";
"letter $(found_letters) is found on the system by CFEngine.";
}
#+end_src

My machine has only two disks, sadly:

#+begin_src text
%cf-agent -KI ./test_findfiles_range.cf
R: /dev/sdb is found on the system by CFEngine.
R: letter /dev/sda is found on the system by CFEngine.
R: letter /dev/sdb is found on the system by CFEngine.
#+end_src

I hope that helps...

Ted
Reply all
Reply to author
Forward
0 new messages