I am starting to experiment with Ruby for Sys Admin tasks. Now, I have
been working with Shell scripting for a few years now. Currently, I am
trying to figure out how I can create a "for loop" in Ruby.
In bash, I have the following: (the following is what I would like to
translate in Ruby but can't seem to figure this out)
BASH - here, I am trying to copy a few files to the same server in one
shot
for groups in "Account File" \
"Bank File" \
"Study Case" \
"Overall Status" ;
do
scp $groups user@server1:/tmp
done
For Ruby, I tried -
for group in Account File",
"Bank File",
"Study Case",
"Overall Status"
do
(scp groups user@server1:/tmp)
end
Of course, I get syntax errors everywhere. I am not sure how to use a
for loop in this case. Any input?
Thanks,
G
--
Posted via http://www.ruby-forum.com/.
[ "Account File", "Bank File", "Study Case", "Overall Status" ].each do |group|
puts "processing #{group}"
end
Hope this helps,
Mike
--
Mike Stok <mi...@stok.ca>
http://www.stok.ca/~mike/
The "`Stok' disclaimers" apply.
I would suggest reading some Ruby tutorials online, because it's
pretty different from Bash, and you often won't want to just translate
things literally. Learning one's second programming language is a big
deal :-)
-Dan
On Thu, Nov 19, 2009 at 2:52 PM, Mike Stok <mi...@stok.ca> wrote:
> > For Ruby, I tried -
> >
> > for group in Account File",
> > "Bank File",
> > "Study Case",
> > "Overall Status"
> > do
> > (scp groups user@server1:/tmp)
> > end
> >
> > Of course, I get syntax errors everywhere. I am not sure how to use a
> > for loop in this case. Any input?
>
> [ "Account File", "Bank File", "Study Case", "Overall Status" ].each do
> |group|
> puts "processing #{group}"
> end
>
Some alternative syntax that might be more comfortable:
for group in [ "Account File", "Bank File", "Study Case", "Overall Status" ]
puts "processing #{group}"
end
for group in "Account File, Bank File, Study Case, Overall Status".split(",
")
Is this possible with Ruby?
For example, in shell, if I had a command `yum -y install package_name`,
I am able to see all of the required packages being installed...pretty
much, I can see the process of it all.
["Account File", "Bank File", "Study Case", "Overall Status"].each |group|
system "scp", group, 'user@server1:/tmp'
end
Assuming all these "groups" are in a single directory you can also do
Dir["#{dir}/*"].each |group|
system "scp", group, 'user@server1:/tmp'
end
Then, if you want to process the output of the command you should look
into IO.popen.
Kind regards
robert
--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
Robert,
Thanks for the input and also in pointing me to IO.popen. I appreciate
the great help from you guys
When you use backticks, the output is captured by ruby and is the
return value of the backtick operator. If you don't want to capture
the output in your program and just want the command's output to go to
standard out then use the
system method.
yum_output = `yum -y install package_name`
system "yum -y install package_name"
Gary Wright