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