Forum: Ruby perl to ruby

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
6286e4461f4efad2528deb283b461435?d=identicon&s=25 kuLa mk (kula)
on 2007-03-13 00:20
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";
> }
97550977337c9f0a0e1a9553e55bfaa0?d=identicon&s=25 Jan Svitok (Guest)
on 2007-03-13 00:52
(Received via mailing list)
On 3/13/07, Marcin Kulisz <marcin.kulisz@gmail.com> 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/, "<br>")
    puts text
else
    puts "wal siê na ryj z³odzieju"
end

alternatively you can do:
    text=`finger #{usr}`.gsub(/\n/, "<br>")
instead of those two lines.

puts appends \n, if you don't like it, use print
6286e4461f4efad2528deb283b461435?d=identicon&s=25 kuLa mk (kula)
on 2007-03-13 08:26
Jan Svitok 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
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2007-03-13 09:10
(Received via mailing list)
On 13.03.2007 08:26, Marcin Kulisz 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
6286e4461f4efad2528deb283b461435?d=identicon&s=25 kuLa mk (kula)
on 2007-03-13 09:21
Robert Klemme 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 :-)
Ae16cb4f6d78e485b04ce1e821592ae5?d=identicon&s=25 Martin DeMello (Guest)
on 2007-03-13 09:36
(Received via mailing list)
On 3/13/07, Jan Svitok <jan.svitok@gmail.com> 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
8cfdf47d27893fbd55d6b9565c34fbe4?d=identicon&s=25 Bruce Woodward (Guest)
on 2007-03-13 09:49
(Received via mailing list)
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/,"<br>")
  else
    "wal sie na rj....."
  end)
Ae16cb4f6d78e485b04ce1e821592ae5?d=identicon&s=25 Martin DeMello (Guest)
on 2007-03-13 09:54
(Received via mailing list)
On 3/13/07, Bruce Woodward <bruce.woodward@gmail.com> 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' :)

martin
56ca88f932c0e0e1fcdcdffd62edad55?d=identicon&s=25 Eckie Silapaswang (eckie)
on 2007-03-13 17:36
Marcin Kulisz wrote:
> Jan Svitok 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
Fd22ee3cfc7dac283ce8e451af324f7d?d=identicon&s=25 Chad Perrin (Guest)
on 2007-03-13 20:44
(Received via mailing list)
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.
Ae16cb4f6d78e485b04ce1e821592ae5?d=identicon&s=25 Martin DeMello (Guest)
on 2007-03-13 20:47
(Received via mailing list)
On 3/14/07, Chad Perrin <perrin@apotheon.com> 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
:) that was supposed to be on one line - didn't realise how long it
had gotten.

m.
Fd22ee3cfc7dac283ce8e451af324f7d?d=identicon&s=25 Chad Perrin (Guest)
on 2007-03-13 20:59
(Received via mailing list)
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
> :) 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.
Ef3aa7f7e577ea8cd620462724ddf73b?d=identicon&s=25 Rob Biedenharn (Guest)
on 2007-03-13 21:24
(Received via mailing list)
On Mar 13, 2007, at 3:59 PM, Chad Perrin 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 Biedenharn    http://agileconsultingllc.com
Rob@AgileConsultingLLC.com
This topic is locked and can not be replied to.