Perl to ruby

hi all,
today is my first day with ruby and I’m trying to rewrite a few of my
scripts from perl to ruby & it’s not too easy. can anyone help me with
script below. I’ve done a few thinks but when one is working another
don’t.

#!/usr/bin/perl -w

$usr=$ARGV[0];
$id=cat /etc/passwd|grep $usr|cut -d\":\" -f3;
if ($id>=1000){
$text=finger $usr;
$text=~s/\n/<br>/g;
print $text;
} else {
print “wal siÄ™ na ryj zÅ‚odzieju”;
}

Jan S. wrote:

#!/usr/bin/ruby -w

usr = ARGV[0]

puts appends \n, if you don’t like it, use print

thx a lot, I was very close to it but had probles with grep, first I
tired to do it with command “system” but it doesn’t working then change
my mind & tired with method “.grep” & it doesn’t work too but your
solution is easy & lovely
again thx for help

On 13.03.2007 08:26, Marcin K. wrote:

my mind & tired with method “.grep” & it doesn’t work too but your
solution is easy & lovely
again thx for help

Also look at this:

http://raa.ruby-lang.org/project/etc/

Kind regards

robert

Robert K. wrote:

Also look at this:

http://raa.ruby-lang.org/project/etc/

hehe thx, I read about it but sometimes my memory is too short :slight_smile:

On 3/13/07, Jan S. [email protected] wrote:

id = cat /etc/passwd|grep #{usr}|cut -d\":\" -f3.to_i
And in pure ruby,

id =
IO.readlines(‘/etc/passwd’).grep(/#{usr}/).first.split(/:/).at(2).to_i

or for efficiency

File.open(“/etc/passwd”, “r”) {|f| id =
f.grep(/#{usr}/).first.split(/:/).at(2).to_i}

which doesn’t read the whole file into an array first.

Also, ideally grep(/#{usr}/) should be
grep(Regexp.new(Regexp.quote(usr))), in case usr contains
special-to-the-regexp-engine characters.

martin

On 3/13/07, Marcin K. [email protected] wrote:

hi all,
today is my first day with ruby and I’m trying to rewrite a few of my
scripts from perl to ruby & it’s not too easy. can anyone help me with
script below. I’ve done a few thinks but when one is working another
don’t.

#!/usr/bin/ruby -w

usr = ARGV[0]
id = cat /etc/passwd|grep #{usr}|cut -d\":\" -f3.to_i
if id>=1000
text=finger #{usr}
text.gsub!(/\n/, “
”)
puts text
else
puts “wal siê na ryj z³odzieju”
end

alternatively you can do:
text=finger #{usr}.gsub(/\n/, “
”)
instead of those two lines.

puts appends \n, if you don’t like it, use print

For efficiency, wouldn’t using the Etc module would be best?

require ‘etc’
user = ARGV[0] or abort “missing user name”
puts (if (Etc.getpwnam(user)).uid >= 1000
finger #{user}.gsub(/\n/,"
")
else
“wal sie na rj…”
end)

On 3/13/07, Bruce W. [email protected] wrote:

For efficiency, wouldn’t using the Etc module would be best?

require ‘etc’
user = ARGV[0] or abort “missing user name”
puts (if (Etc.getpwnam(user)).uid >= 1000

touche’ :slight_smile:

martin

Marcin K. wrote:

Jan S. wrote:

#!/usr/bin/ruby -w

usr = ARGV[0]

puts appends \n, if you don’t like it, use print

thx a lot, I was very close to it but had probles with grep, first I
tired to do it with command “system” but it doesn’t working then change
my mind & tired with method “.grep” & it doesn’t work too but your
solution is easy & lovely
again thx for help

Just for future reference (and I found this out the hard way by having
it not working on me either) the
system(“insert_your_shell_command_here”) method will return a boolean if
your shell can execute the command (1 or 0, true or false). You can use
the grep command, but as you saw in the solution, you use backticks
to have system commands in your ruby script.

Best regards,

Eckie

On 3/14/07, Chad P. [email protected] wrote:

id = f.grep(/#{usr}).first.split(/:/).at(2).to_i

end

I’m sure greater readability can be had by rewriting that entirely, but
I found the multiline braces approach suboptimal, personally. YMMV.

And the fact that my paste put a line break where it did didn’t help
:slight_smile: that was supposed to be on one line - didn’t realise how long it
had gotten.

m.

On Tue, Mar 13, 2007 at 05:36:20PM +0900, Martin DeMello wrote:

or for efficiency

File.open("/etc/passwd", “r”) {|f| id =
f.grep(/#{usr}/).first.split(/:/).at(2).to_i}

. . . or for more readability:

File.open("/etc/passwd", “r”) do |f|
id = f.grep(/#{usr}).first.split(/:/).at(2).to_i
end

I’m sure greater readability can be had by rewriting that entirely, but
I found the multiline braces approach suboptimal, personally. YMMV.

One could also split up that string of methods into more than one line
via assignment to variables, but I’m lazy enough to prefer to spend
twice as much effort talking about why I didn’t.

On Mar 13, 2007, at 3:59 PM, Chad P. wrote:

had gotten.
Lucid

File.open(“/etc/passwd”, “r”) do |f|
id = f.detect {|line| line =~ /\A#{usr}:/}.split(/:/)[2].to_i
end

using grep will read all the lines, but detect will stop after
finding the first line for which the block is true. I also suggest
bracketing your regexp with \A (beginning of string) and
‘:’ (separator in /etc/passwd) so searching for, say, “0”, doesn’t
give you the root account.

You probably also want to rescue exceptions that will arise if the
usr is not found.

-Rob

Rob B. http://agileconsultingllc.com
[email protected]

On Wed, Mar 14, 2007 at 04:46:43AM +0900, Martin DeMello wrote:

File.open("/etc/passwd", “r”) do |f|
id = f.grep(/#{usr}).first.split(/:/).at(2).to_i
end

I’m sure greater readability can be had by rewriting that entirely, but
I found the multiline braces approach suboptimal, personally. YMMV.

And the fact that my paste put a line break where it did didn’t help
:slight_smile: that was supposed to be on one line - didn’t realise how long it
had gotten.

Hmm. It should have occurred to me that was meant to be all one line.

I think lines that long should usually be broken up for readability
anyway. Usually.