looping through array elements (noob question)

Discussion in 'Ruby' started by Nate St.germain, Jun 1, 2010.

  1. please bear with me on this. i'm blundering my way through learning
    ruby.

    i have a script intended to run on os x to restore local admin rights to
    users who've had the rights removed by another script. it's sort of a
    fallback measure. i'm using this as an opportunity to learn ruby in the
    process.

    the newest version of the removal script (bash) will add a plain text
    file in /Library/Receipts containing the account short names on which it
    just operated. i want the restore script to be flexible enough to either
    read the list from the file if it exists or get the users from scratch
    using the "/usr/sbin/jamf listUser" command (part of the Casper admin
    suite), which outputs xml, unfortunately. at some point, i'll probably
    switch to another method to avoid having to resort to regex matches on
    the xml tags.

    regardless, the issue is the getUsers() method works fine on its own but
    then give me the following output when run from the script (below):

    sam
    norton
    mary
    Restoring admin rights for <name>sam</name>
    Restoring admin rights for <name>norton</name>
    Restoring admin rights for <name>mary</name>

    so in the actual script, the names are returned once correctly, then
    again with the xml tags and tabs intact. i'm not sure what i'm missing
    here.

    it's probably something obvious, and i'm sure this script can be written
    more concisely. i appreciate any pointers you have. note, the actual
    admin rights granting lines are commented below for testing.


    === script ===

    #!/usr/bin/env ruby
    # restore rights to local users who've recently had them removed.
    # 5/25/10, nate@tsp
    # 6/1/10, updated

    # set system variable
    # chops out the second digit in the version number, which is the only
    differentiating factor here
    def getos()
    system=`/usr/bin/sw_vers
    -productVersion`.chomp.split(".").slice(1).to_i
    if system==4 then
    return "tiger"
    else
    return "leo"
    end
    end

    # cheating by using the jamf binary.
    def getUsers()
    userlist=`/usr/sbin/jamf listUsers`.to_a
    users=userlist.grep /\<name/
    users.each { |user| puts user.split(/<[^>]*[^>]*>/)[1] }
    end

    # method to read admins from a text file from a removal run
    # this may not be necessary if included in the restoreAdmin methods
    def readAdmins()
    receipt=File.open('/Library/Receipts/org.company.removedadmins', 'r')
    return receipt.readlines
    end

    # use dseditgroup for 10.[5-6] clients
    def restoreAdmin5()
    if File.exist?('/Library/Receipts/org.company.removedadmins') then
    users=readAdmins
    users.each do |u|
    puts "Restoring admin rights for #{u}"
    # %x(/usr/sbin/dseditgroup -o edit -a #{u} -t user admin)
    end
    else
    users=getUsers
    users.each do |u|
    puts "Restoring admin rights for #{u}"
    # %x(/usr/sbin/dseditgroup -o edit -a #{u} -t user admin)
    end
    end
    end

    # use nicl for 10.4 clients
    def restoreAdmin4()
    if File.exist?('/Library/Receipts/org.company.removedadmins') then
    users=readAdmins
    users.each do |u|
    puts "Restoring admin rights for #{u}"
    # %x(nicl -raw /var/db/netinfo/local.nidb -append /groups/admin
    users #{u})
    end
    else
    users=getUsers
    users.each do |u|
    puts "Restoring admin rights for #{u}"
    # %x(nicl -raw /var/db/netinfo/local.nidb -append /groups/admin users
    #{u})
    end
    end
    end

    # test the os with the getos() method and proceed accordingly based on
    platform
    result = case getos()
    when "tiger" then
    restoreAdmin4
    when "leo" then
    restoreAdmin5
    else puts "no version specified. stopping..."
    end
    --
    Posted via http://www.ruby-forum.com/.
     
    Nate St.germain, Jun 1, 2010
    #1
    1. Advertising

  2. 2010/6/1 Nate St.germain <>:
    > please bear with me on this. i'm blundering my way through learning
    > ruby.
    >
    > i have a script intended to run on os x to restore local admin rights to
    > users who've had the rights removed by another script. it's sort of a
    > fallback measure. i'm using this as an opportunity to learn ruby in the
    > process.
    >
    > the newest version of the removal script (bash) will add a plain text
    > file in /Library/Receipts containing the account short names on which it
    > just operated. i want the restore script to be flexible enough to either
    > read the list from the file if it exists or get the users from scratch
    > using the "/usr/sbin/jamf listUser" command (part of the Casper admin
    > suite), which outputs xml, unfortunately. at some point, i'll probably
    > switch to another method to avoid having to resort to regex matches on
    > the xml tags.
    >
    > regardless, the issue is the getUsers() method works fine on its own but
    > then give me the following output when run from the script (below):
    >
    > sam
    > norton
    > mary
    > Restoring admin rights for =A0 =A0 <name>sam</name>
    > Restoring admin rights for =A0 =A0 <name>norton</name>
    > Restoring admin rights for =A0 =A0 <name>mary</name>
    >
    > so in the actual script, the names are returned once correctly, then
    > again with the xml tags and tabs intact. i'm not sure what i'm missing
    > here.
    >
    > it's probably something obvious, and i'm sure this script can be written
    > more concisely. i appreciate any pointers you have. note, the actual
    > admin rights granting lines are commented below for testing.
    >
    >
    > =3D=3D=3D script =3D=3D=3D
    >
    > #!/usr/bin/env ruby
    > # restore rights to local users who've recently had them removed.
    > # 5/25/10, nate@tsp
    > # 6/1/10, updated
    >
    > # set system variable
    > # chops out the second digit in the version number, which is the only
    > differentiating factor here
    > def getos()
    > =A0system=3D`/usr/bin/sw_vers
    > -productVersion`.chomp.split(".").slice(1).to_i
    > =A0if system=3D=3D4 then
    > =A0 =A0return "tiger"
    > =A0else
    > =A0 =A0 =A0return "leo"
    > =A0end
    > end
    >
    > # cheating by using the jamf binary.
    > def getUsers()
    > =A0userlist=3D`/usr/sbin/jamf listUsers`.to_a
    > =A0users=3Duserlist.grep /\<name/
    > =A0users.each { |user| puts user.split(/<[^>]*[^>]*>/)[1] }


    The line above produces the output of the short names - and then it
    returns "users" *unmodified*. This means that the array returned from
    getUsers() still has all the XML tags in there.

    You can replace that line with:

    users.map { |user| user.split(/<[^>]*[^>]*>/)[1] }

    Or, with an alternative approach

    def get_users
    users =3D []
    `/usr/sbin/jamf listUsers`.scan(/<name>(.*?)</name>/) { users << $1 }
    users
    end

    Or even use an XML tool (e.g. REXML) to extract values of tags if the
    input is valid XML.

    Note: conventionally we use CamelCase only for ClassNames and not for
    method_names.

    > end
    >
    > # method to read admins from a text file from a removal run
    > # this may not be necessary if included in the restoreAdmin methods
    > def readAdmins()
    > =A0receipt=3DFile.open('/Library/Receipts/org.company.removedadmins', 'r'=

    )
    > =A0return receipt.readlines
    > end
    >
    > # use dseditgroup for 10.[5-6] clients
    > def restoreAdmin5()
    > =A0if File.exist?('/Library/Receipts/org.company.removedadmins') then
    > =A0 =A0users=3DreadAdmins
    > =A0 =A0users.each do |u|
    > =A0 =A0puts "Restoring admin rights for #{u}"
    > =A0# =A0%x(/usr/sbin/dseditgroup -o edit -a =A0#{u} -t user admin)
    > =A0 =A0end
    > =A0else
    > =A0 =A0users=3DgetUsers
    > =A0 =A0users.each do |u|
    > =A0 =A0puts "Restoring admin rights for #{u}"
    > =A0# =A0%x(/usr/sbin/dseditgroup -o edit -a =A0#{u} -t user admin)
    > =A0 =A0end
    > =A0end
    > end
    >
    > # use nicl for 10.4 clients
    > def restoreAdmin4()
    > =A0if File.exist?('/Library/Receipts/org.company.removedadmins') then
    > =A0 =A0users=3DreadAdmins
    > =A0 =A0users.each do |u|
    > =A0 =A0 =A0puts "Restoring admin rights for #{u}"
    > =A0# =A0 =A0%x(nicl -raw /var/db/netinfo/local.nidb -append /groups/admin
    > users #{u})
    > =A0end
    > =A0else
    > =A0 =A0users=3DgetUsers
    > =A0 =A0users.each do |u|
    > =A0 =A0puts "Restoring admin rights for #{u}"
    > =A0# =A0%x(nicl -raw /var/db/netinfo/local.nidb -append /groups/admin use=

    rs
    > #{u})
    > =A0 =A0end
    > =A0end
    > end
    >
    > # test the os with the getos() method and proceed accordingly based on
    > platform
    > result =3D case getos()
    > =A0when "tiger" then
    > =A0 =A0restoreAdmin4
    > =A0when "leo" then
    > =A0 =A0restoreAdmin5
    > =A0else puts "no version specified. stopping..."
    > =A0end


    Kind regards

    robert


    --=20
    remember.guy do |as, often| as.you_can - without end
    http://blog.rubybestpractices.com/
     
    Robert Klemme, Jun 1, 2010
    #2
    1. Advertising

  3. Robert Klemme wrote:
    > Note: conventionally we use CamelCase only for ClassNames and not for
    > method_names.
    >
    > Kind regards
    >
    > robert


    thanks, robert. i'll give that a shot. the camel case is a holdover from
    way back. i appreciate the tips.
    --
    Posted via http://www.ruby-forum.com/.
     
    Nate St.Germain, Jun 1, 2010
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Steven K

    Looping through form elements

    Steven K, Nov 12, 2003, in forum: ASP General
    Replies:
    3
    Views:
    112
    Randy Rahbar
    Nov 13, 2003
  2. dkmd_nielsen
    Replies:
    5
    Views:
    170
    dkmd_nielsen
    Mar 8, 2007
  3. Mel

    Looping through all Form Elements

    Mel, Aug 16, 2006, in forum: Javascript
    Replies:
    1
    Views:
    124
    Georgi Naumov
    Aug 16, 2006
  4. Aaron
    Replies:
    2
    Views:
    577
    dhtml
    Apr 10, 2011
  5. Replies:
    5
    Views:
    304
Loading...

Share This Page