Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

For Loop

0 views
Skip to first unread message

Gilberto Gilberto

unread,
Nov 19, 2009, 3:38:51 PM11/19/09
to
Hello All,

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

Mike Stok

unread,
Nov 19, 2009, 3:52:43 PM11/19/09
to


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

Dan Q

unread,
Nov 19, 2009, 3:56:19 PM11/19/09
to
I would add that you can use backticks to execute a shell command, as follows:
`scp "#{group}" user@server1:/tmp`

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

Josh Cheek

unread,
Nov 19, 2009, 4:08:08 PM11/19/09
to
[Note: parts of this message were removed to make it a legal post.]

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(",
")

Unix4Linux Unix4Linux

unread,
Nov 19, 2009, 4:33:56 PM11/19/09
to
Thank you all...that is what I was looking for. I have two books and I
did some searches on google, but the examples were not clear.

Unix4Linux Unix4Linux

unread,
Nov 19, 2009, 4:41:06 PM11/19/09
to
I also forgot to ask, in SHELL, when I run shell scripts, I can see what
the script is doing. I am not referring to debug mode but more the
output of each command being executed.

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.

Robert Klemme

unread,
Nov 20, 2009, 2:28:17 AM11/20/09
to

["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/

Unix4Linux Unix4Linux

unread,
Nov 20, 2009, 7:42:30 AM11/20/09
to

Robert,

Thanks for the input and also in pointing me to IO.popen. I appreciate
the great help from you guys

Gary Wright

unread,
Nov 20, 2009, 11:04:12 AM11/20/09
to

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

0 new messages