Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > looping through array elements (noob question)

Reply
Thread Tools

looping through array elements (noob question)

 
 
Nate St.germain
Guest
Posts: n/a
 
      06-01-2010
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/.

 
Reply With Quote
 
 
 
 
Robert Klemme
Guest
Posts: n/a
 
      06-01-2010
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/

 
Reply With Quote
 
 
 
 
Nate St.Germain
Guest
Posts: n/a
 
      06-01-2010
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/.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
looping in array vs looping in a dic giuseppe.amatulli@gmail.com Python 5 09-20-2012 11:58 PM
looping through json array loops through the characters instead ofthe values Aaron Javascript 2 04-10-2011 05:58 PM
Looping through array, deleting elements dkmd_nielsen Ruby 5 03-08-2007 09:42 PM
Looping through all Form Elements Mel Javascript 1 08-16-2006 08:12 PM
Looping through form elements Steven K ASP General 3 11-13-2003 02:23 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57