Newbie want help

I have tried to rewrite an old short Perl script, that reads a Unix
password file, and prints loginname and realname if the userid number
is greater than 100. The Perl script is here:

#!/usr/bin/perl

open (PASSWD,"/etc/passwd") || die (“Can’t open passwd file!”);
@passwd = ;
close (PASSWD);

foreach (@passwd) {
@userdata = split(/:/,$_);
if (@userdata[0] =~ /^#/) {
next;
}

    if (@userdata[2] > 100) {
            printf "%12s %30s\n", @userdata[0], @userdata[4];
    }

}

Script ends

I have been reading “Programming Ruby” written by The Pragmatic
Programmers, and
I have no trouble opening the password file, and I can also print the
file content to the screen. I am having a problem writing the loop the
"Ruby way".

I will appreciate, if someone takes the time and helps me getting
further with my attempts on programming Ruby.

/Allan

File.foreach("/etc/passwd") do |line|
  next if line =~ /^\s*#/
  userdata = line.chomp.split(/:/)
  puts("%12s %30s" % [userdata[0], userdata[4]]) if userdata[2].to_i > 100
end

Regards,

  Michael

···

On Thu, Mar 18, 2004 at 07:54:33PM +0900, Allan Wermuth wrote:

I have tried to rewrite an old short Perl script, that reads a Unix
password file, and prints loginname and realname if the userid number
is greater than 100. The Perl script is here:

#!/usr/bin/perl

open (PASSWD,"/etc/passwd") || die ("Can't open passwd file!");
@passwd = <PASSWD>;
close (PASSWD);

foreach (@passwd) {
        @userdata = split(/:/,$_);
        if (@userdata[0] =~ /^#/) {
                next;
        }
                
        if (@userdata[2] > 100) {
                printf "%12s %30s\n", @userdata[0], @userdata[4];
        }
}

# Script ends

untested:
open(‘/etc/passwd’) do |fd|
fd.each do |line|
data=line.split(‘:’)
next if data[0] =~ /^#/
printf “%12s %30s\n”, data[0], data[4] if data[2].to_i >100
end
end

···

il 18 Mar 2004 02:50:45 -0800, alw@it-service.sdu.dk (Allan Wermuth) ha scritto::

I have tried to rewrite an old short Perl script, that reads a Unix
password file, and prints loginname and realname if the userid number
is greater than 100. The Perl script is here:

I have been reading “Programming Ruby” written by The Pragmatic
Programmers, and
I have no trouble opening the password file, and I can also print the
file content to the screen. I am having a problem writing the loop the
“Ruby way”.

“Allan Wermuth” alw@it-service.sdu.dk schrieb im Newsbeitrag
news:c2d6f318.0403180250.200a498b@posting.google.com

I have tried to rewrite an old short Perl script, that reads a Unix
password file, and prints loginname and realname if the userid number
is greater than 100. The Perl script is here:

#!/usr/bin/perl

open (PASSWD,“/etc/passwd”) || die (“Can’t open passwd file!”);
@passwd = ;
close (PASSWD);

foreach (@passwd) {
@userdata = split(/:/,$_);
if (@userdata[0] =~ /^#/) {
next;
}

    if (@userdata[2] > 100) {
            printf "%12s %30s\n", @userdata[0], @userdata[4];
    }

}

Script ends

I have been reading “Programming Ruby” written by The Pragmatic
Programmers, and
I have no trouble opening the password file, and I can also print the
file content to the screen. I am having a problem writing the loop the
“Ruby way”.

I will appreciate, if someone takes the time and helps me getting
further with my attempts on programming Ruby.

Just for the fun of it you can make it a one liner:

ruby -naF: -e ‘printf(“%12s %30s\n”, $F[0], $F[4]) if ! /^\s*#/ &&
$F[2].to_i > 100’ /etc/passwd
ruby -naF: -e ‘printf(“%12s %30s\n”, $F[0], $F[4]) unless /^\s*#/ ||
$F[2].to_i <= 100’ /etc/passwd

:slight_smile:

“-n” does the looping without printing, “-a” switches on autosplit into
$F, “-F:” says that “:” is the split expression and “-e” executes the
given expression for each line.

13:48:09 [aaa]: ruby -h
Usage: ruby [switches] [–] [programfile] [arguments]
-0[octal] specify record separator (\0, if no argument)
-a autosplit mode with -n or -p (splits $_ into $F)
-c check syntax only
-Cdirectory cd to directory, before executing your script
-d set debugging flags (set $DEBUG to true)
-e ‘command’ one line of script. Several -e’s allowed. Omit
[programfile]
-Fpattern split() pattern for autosplit (-a)
-i[extension] edit ARGV files in place (make backup if extension
supplied)
-Idirectory specify $LOAD_PATH directory (may be used more than
once)
-Kkcode specifies KANJI (Japanese) code-set
-l enable line ending processing
-n assume ‘while gets(); … end’ loop around your script
-p assume loop like -n but print line also like sed
-rlibrary require the library, before executing your script
-s enable some switch parsing for switches after script
name
-S look for the script using PATH environment variable
-T[level] turn on tainting checks
-v print version number, then turn on verbose mode
-w turn warnings on for your script
-W[level] set warning level; 0=silence, 1=medium, 2=verbose
(default)
-x[directory] strip off text before #!ruby line and perhaps cd to
directory
–copyright print the copyright
–version print the version

Regards

robert

Allan Wermuth wrote:

I have tried to rewrite an old short Perl script, that reads a Unix
password file, and prints loginname and realname if the userid number
is greater than 100. The Perl script is here:

require 'etc'
Etc.passwd {|p|  puts "#{p.name} #{p.comment}" if p.uid > 100}

Although 1.8 says ‘comment’ is undefined method here on OS X …

gabriele renzi wrote:

···

il 18 Mar 2004 02:50:45 -0800, alw@it-service.sdu.dk (Allan Wermuth) > ha scritto::

untested:
open(‘/etc/passwd’) do |fd|
fd.each do |line|
data=line.split(‘:’)
next if data[0] =~ /^#/
printf “%12s %30s\n”, data[0], data[4] if data[2].to_i >100
end
end

Thank You very much for your help.

It works :slight_smile:

/Allan

Allan Wermuth wrote:

I have tried to rewrite an old short Perl script, that reads a Unix
password file, and prints loginname and realname if the userid number
is greater than 100. The Perl script is here:

require 'etc'
Etc.passwd {|p|  puts "#{p.name} #{p.comment}" if p.uid > 100}

Although 1.8 says ‘comment’ is undefined method here on OS X …

A bit off topic, But I think one should use getent to enumerate users rather than the passwd file. Parsing anyhow will be needed.