For Loop

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

On Nov 19, 2009, at 3:38 PM, Gilberto Gilberto wrote:

shot
For Ruby, I tried -
for loop in this case. Any input?
[ “Account File”, “Bank File”, “Study Case”, “Overall Status” ].each do
|group|
puts “processing #{group}”
end

Hope this helps,

Mike

Mike S. [email protected]
http://www.stok.ca/~mike/

The “`Stok’ disclaimers” apply.

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 :slight_smile:

-Dan

On Thu, Nov 19, 2009 at 2:52 PM, Mike S. [email protected] wrote:

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(",
")
puts “processing #{group}”
end

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.

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 K. wrote:

On 19.11.2009 22:41, Unix4Linux Unix4Linux wrote:

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.

[“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

Robert,

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

On 19.11.2009 22:41, Unix4Linux Unix4Linux wrote:

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.

[“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

On Nov 19, 2009, at 4:41 PM, Unix4Linux Unix4Linux wrote:

much, I can see the process of it all.
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 W.