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";
}
--
Posted via http://www.ruby-forum.com/\.
#!/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
···
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.
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
···
--
Posted via http://www.ruby-forum.com/\.
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, Jan Svitok <jan.svitok@gmail.com> wrote:
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
Also look at this:
http://raa.ruby-lang.org/project/etc/
Kind regards
robert
···
On 13.03.2007 08:26, 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
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)
···
On 3/13/07, Martin DeMello <martindemello@gmail.com> wrote:
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}
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
···
--
Posted via http://www.ruby-forum.com/\.
. . . 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 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}
--
CCD CopyWrite Chad Perrin [ http://ccd.apotheon.org ]
Leon Festinger: "A man with a conviction is a hard man to change. Tell
him you disagree and he turns away. Show him facts and figures and he
questions your sources. Appeal to logic and he fails to see your point."
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 
···
--
Posted via http://www.ruby-forum.com/\.
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.
···
On 3/14/07, Chad Perrin <perrin@apotheon.com> wrote:
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.
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.
···
On Wed, Mar 14, 2007 at 04:46:43AM +0900, Martin DeMello wrote:
On 3/14/07, Chad Perrin <perrin@apotheon.com> wrote:
>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.
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.
--
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
Rob@AgileConsultingLLC.com
···
On Mar 13, 2007, at 3:59 PM, Chad Perrin wrote:
On Wed, Mar 14, 2007 at 04:46:43AM +0900, Martin DeMello wrote:
On 3/14/07, Chad Perrin <perrin@apotheon.com> wrote:
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.
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