perl to ruby

M

Marcin Kulisz

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";
}
 
J

Jan Svitok

T24gMy8xMy8wNywgTWFyY2luIEt1bGlzeiA8bWFyY2luLmt1bGlzekBnbWFpbC5jb20+IHdyb3Rl
Ogo+IGhpIGFsbCwKPiB0b2RheSBpcyBteSBmaXJzdCBkYXkgd2l0aCBydWJ5IGFuZCBJJ20gdHJ5
aW5nIHRvIHJld3JpdGUgYSBmZXcgb2YgbXkKPiBzY3JpcHRzIGZyb20gcGVybCB0byBydWJ5ICYg
aXQncyBub3QgdG9vIGVhc3kuIGNhbiBhbnlvbmUgaGVscCBtZSB3aXRoCj4gc2NyaXB0IGJlbG93
LiBJJ3ZlIGRvbmUgYSBmZXcgdGhpbmtzIGJ1dCB3aGVuIG9uZSBpcyB3b3JraW5nIGFub3RoZXIK
PiBkb24ndC4KCiMhL3Vzci9iaW4vcnVieSAtdwoKdXNyID0gQVJHVlswXQppZCA9IGBjYXQgL2V0
Yy9wYXNzd2R8Z3JlcCAje3Vzcn18Y3V0IC1kXCI6XCIgLWYzYC50b19pCmlmIGlkPj0xMDAwCiAg
ICB0ZXh0PWBmaW5nZXIgI3t1c3J9YAogICAgdGV4dC5nc3ViISgvXG4vLCAiPGJyPiIpCiAgICBw
dXRzIHRleHQKZWxzZQogICAgcHV0cyAid2FsIHNp6iBuYSByeWogerNvZHppZWp1IgplbmQKCmFs
dGVybmF0aXZlbHkgeW91IGNhbiBkbzoKICAgIHRleHQ9YGZpbmdlciAje3Vzcn1gLmdzdWIoL1xu
LywgIjxicj4iKQppbnN0ZWFkIG9mIHRob3NlIHR3byBsaW5lcy4KCnB1dHMgYXBwZW5kcyBcbiwg
aWYgeW91IGRvbid0IGxpa2UgaXQsIHVzZSBwcmludAo=
 
M

Marcin Kulisz

Jan said:
#!/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
 
R

Robert Klemme

Jan said:
#!/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

Also look at this:

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

Kind regards

robert
 
M

Martin DeMello

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

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
 
M

Martin DeMello

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
 
E

Eckie Silapaswang

Marcin said:
Jan said:
#!/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
 
C

Chad Perrin

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.
 
M

Martin DeMello

. . . 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.

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.
 
C

Chad Perrin

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.
 
R

Rob Biedenharn

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.

--
CCD CopyWrite Chad Perrin [ http://ccd.apotheon.org ]
"The first rule of magic is simple. Don't waste your time waving your
hands and hopping when a rock or a club will do." - McCloctnick the
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
(e-mail address removed)
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top