Easy way to put integers into a variable

28 views
Skip to first unread message

bofh...@gmail.com

unread,
Jan 4, 2017, 7:20:48 PM1/4/17
to help-cfengine
I have been looking and I don't see an easy way to do this.  Basically I am looking to create a lock file for all /tmp/.X* files used by VNC.  These files are named /tmp/.X1-lock all the way to .X99-lock.

What is the easy way to create the file starting at 1 and ending at 99.  I need to specify an integer range to a variable so I can loop and create a file for each file.


Sample bundle:

bundle agent create_x11_lock
{
  vars:
   "counter"  ilist => { 1..99 };     =====  I need this to allow a range from 1 to 99.  I hate to enter "1", "2",......

  files:
   "/tmp/.X$(counter)-lock"
     create    => "true",
     perms     => m("644");
}


Thank you in advance,
Robin Friedrich

nick.a...@cfengine.com

unread,
Jan 4, 2017, 8:01:15 PM1/4/17
to help-cfengine, bofh...@gmail.com
I think that expandrange (introduced in 3.7) will be your friend here.

https://docs.cfengine.com/docs/master/reference-functions-expandrange.html

Sent from my android device.
--
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.

Nick Anderson

unread,
Jan 4, 2017, 9:36:18 PM1/4/17
to bofh...@gmail.com, help-cfengine, Nick Anderson

bofh...@gmail.com writes:

> What is the easy way to create the file starting at 1 and ending at 99. I
> need to specify an integer range to a variable so I can loop and create a
> file for each file.

Hey Robin,

I am curious why you want to create these lock files. Care to share?

bundle agent main
{
  vars:
    "counter" slist => expandrange("[1-99]", 1);

  files:
    "/tmp/.X$(counter)-lock" -> { "bofh2703" }
      create => "true",
      action => warn_only,
      perms => m("0644");
}
warning: Warning promised, need to create file '/tmp/.X1-lock'
warning: Warning promised, need to create file '/tmp/.X2-lock'
warning: Warning promised, need to create file '/tmp/.X3-lock'
warning: Warning promised, need to create file '/tmp/.X4-lock'
warning: Warning promised, need to create file '/tmp/.X5-lock'
warning: Warning promised, need to create file '/tmp/.X6-lock'
warning: Warning promised, need to create file '/tmp/.X7-lock'
warning: Warning promised, need to create file '/tmp/.X8-lock'
warning: Warning promised, need to create file '/tmp/.X9-lock'
warning: Warning promised, need to create file '/tmp/.X10-lock'
warning: Warning promised, need to create file '/tmp/.X11-lock'
warning: Warning promised, need to create file '/tmp/.X12-lock'
warning: Warning promised, need to create file '/tmp/.X13-lock'
warning: Warning promised, need to create file '/tmp/.X14-lock'
warning: Warning promised, need to create file '/tmp/.X15-lock'
warning: Warning promised, need to create file '/tmp/.X16-lock'
warning: Warning promised, need to create file '/tmp/.X17-lock'
warning: Warning promised, need to create file '/tmp/.X18-lock'
warning: Warning promised, need to create file '/tmp/.X19-lock'
warning: Warning promised, need to create file '/tmp/.X20-lock'
warning: Warning promised, need to create file '/tmp/.X21-lock'
warning: Warning promised, need to create file '/tmp/.X22-lock'
warning: Warning promised, need to create file '/tmp/.X23-lock'
warning: Warning promised, need to create file '/tmp/.X24-lock'
warning: Warning promised, need to create file '/tmp/.X25-lock'
warning: Warning promised, need to create file '/tmp/.X26-lock'
warning: Warning promised, need to create file '/tmp/.X27-lock'
warning: Warning promised, need to create file '/tmp/.X28-lock'
warning: Warning promised, need to create file '/tmp/.X29-lock'
warning: Warning promised, need to create file '/tmp/.X30-lock'
warning: Warning promised, need to create file '/tmp/.X31-lock'
warning: Warning promised, need to create file '/tmp/.X32-lock'
warning: Warning promised, need to create file '/tmp/.X33-lock'
warning: Warning promised, need to create file '/tmp/.X34-lock'
warning: Warning promised, need to create file '/tmp/.X35-lock'
warning: Warning promised, need to create file '/tmp/.X36-lock'
warning: Warning promised, need to create file '/tmp/.X37-lock'
warning: Warning promised, need to create file '/tmp/.X38-lock'
warning: Warning promised, need to create file '/tmp/.X39-lock'
warning: Warning promised, need to create file '/tmp/.X40-lock'
warning: Warning promised, need to create file '/tmp/.X41-lock'
warning: Warning promised, need to create file '/tmp/.X42-lock'
warning: Warning promised, need to create file '/tmp/.X43-lock'
warning: Warning promised, need to create file '/tmp/.X44-lock'
warning: Warning promised, need to create file '/tmp/.X45-lock'
warning: Warning promised, need to create file '/tmp/.X46-lock'
warning: Warning promised, need to create file '/tmp/.X47-lock'
warning: Warning promised, need to create file '/tmp/.X48-lock'
warning: Warning promised, need to create file '/tmp/.X49-lock'
warning: Warning promised, need to create file '/tmp/.X50-lock'
warning: Warning promised, need to create file '/tmp/.X51-lock'
warning: Warning promised, need to create file '/tmp/.X52-lock'
warning: Warning promised, need to create file '/tmp/.X53-lock'
warning: Warning promised, need to create file '/tmp/.X54-lock'
warning: Warning promised, need to create file '/tmp/.X55-lock'
warning: Warning promised, need to create file '/tmp/.X56-lock'
warning: Warning promised, need to create file '/tmp/.X57-lock'
warning: Warning promised, need to create file '/tmp/.X58-lock'
warning: Warning promised, need to create file '/tmp/.X59-lock'
warning: Warning promised, need to create file '/tmp/.X60-lock'
warning: Warning promised, need to create file '/tmp/.X61-lock'
warning: Warning promised, need to create file '/tmp/.X62-lock'
warning: Warning promised, need to create file '/tmp/.X63-lock'
warning: Warning promised, need to create file '/tmp/.X64-lock'
warning: Warning promised, need to create file '/tmp/.X65-lock'
warning: Warning promised, need to create file '/tmp/.X66-lock'
warning: Warning promised, need to create file '/tmp/.X67-lock'
warning: Warning promised, need to create file '/tmp/.X68-lock'
warning: Warning promised, need to create file '/tmp/.X69-lock'
warning: Warning promised, need to create file '/tmp/.X70-lock'
warning: Warning promised, need to create file '/tmp/.X71-lock'
warning: Warning promised, need to create file '/tmp/.X72-lock'
warning: Warning promised, need to create file '/tmp/.X73-lock'
warning: Warning promised, need to create file '/tmp/.X74-lock'
warning: Warning promised, need to create file '/tmp/.X75-lock'
warning: Warning promised, need to create file '/tmp/.X76-lock'
warning: Warning promised, need to create file '/tmp/.X77-lock'
warning: Warning promised, need to create file '/tmp/.X78-lock'
warning: Warning promised, need to create file '/tmp/.X79-lock'
warning: Warning promised, need to create file '/tmp/.X80-lock'
warning: Warning promised, need to create file '/tmp/.X81-lock'
warning: Warning promised, need to create file '/tmp/.X82-lock'
warning: Warning promised, need to create file '/tmp/.X83-lock'
warning: Warning promised, need to create file '/tmp/.X84-lock'
warning: Warning promised, need to create file '/tmp/.X85-lock'
warning: Warning promised, need to create file '/tmp/.X86-lock'
warning: Warning promised, need to create file '/tmp/.X87-lock'
warning: Warning promised, need to create file '/tmp/.X88-lock'
warning: Warning promised, need to create file '/tmp/.X89-lock'
warning: Warning promised, need to create file '/tmp/.X90-lock'
warning: Warning promised, need to create file '/tmp/.X91-lock'
warning: Warning promised, need to create file '/tmp/.X92-lock'
warning: Warning promised, need to create file '/tmp/.X93-lock'
warning: Warning promised, need to create file '/tmp/.X94-lock'
warning: Warning promised, need to create file '/tmp/.X95-lock'
warning: Warning promised, need to create file '/tmp/.X96-lock'
warning: Warning promised, need to create file '/tmp/.X97-lock'
warning: Warning promised, need to create file '/tmp/.X98-lock'
warning: Warning promised, need to create file '/tmp/.X99-lock'


Nick Anderson
Doer of things, CFEngine

Nick Anderson

unread,
Jan 4, 2017, 9:41:05 PM1/4/17
to bofh...@gmail.com, help-cfengine

Nick Anderson writes:

> #+BEGINSRC cfengine3


> bundle agent main
> {
> vars:
> "counter" slist => expandrange("[1-99]", 1);
>
> files:

> "tmp.X$(counter)-lock" -> { "bofh2703" }
> create => "true",
> action => warnonly,


> perms => m("0644");
> }

> #+ENDSRC
>

I think this is actually a better declaration, and works equally as
well.

bundle agent main
{
  vars:
    "vnc_lock_files" slist => expandrange("/tmp/.X[1-99]-lock", 1);

  files:
    "$(vnc_lock_files)" -> { "bofh2703" }

bofh...@gmail.com

unread,
Jan 5, 2017, 12:15:18 PM1/5/17
to help-cfengine
I am creating the locks to prevent users from starting additional VNC sessions.  We use xinetd to start VNC sessions and want to restrict users from starting additional VNC sessions.  If we create the lock files then users are prevented from starting more VNC sessions.  We have been moving to Exceed OnDemand so this is only a band-aid for now.

There might be a better way to prevent users from multiple VNC sessions but this is what this company already had setup.

Thank everyone for the help. 

-Robin

Nick Anderson

unread,
Jan 5, 2017, 12:48:55 PM1/5/17
to help-cfengine
On Thursday, January 5, 2017 at 11:15:18 AM UTC-6, bofh...@gmail.com wrote:
I am creating the locks to prevent users from starting additional VNC sessions.  We use xinetd to start VNC sessions and want to restrict users from starting additional VNC sessions.  If we create the lock files then users are prevented from starting more VNC sessions.  We have been moving to Exceed OnDemand so this is only a band-aid for now

Thanks for the context Robin.

Hope it works out well for you.
 
Reply all
Reply to author
Forward
0 new messages