For Loop

G

Gilberto Gilberto

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
 
M

Mike Stok

Hello All,
=20
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.
=20
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)
=20
BASH - here, I am trying to copy a few files to the same server in one
shot
=20
for groups in "Account File" \
"Bank File" \
"Study Case" \
"Overall Status" ;
do
scp $groups user@server1:/tmp
done
=20
=20
For Ruby, I tried -
=20
for group in Account File",
"Bank File",
"Study Case",
"Overall Status"
do
(scp groups user@server1:/tmp)
end
=20
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

Hope this helps,

Mike

--=20

Mike Stok <[email protected]>
http://www.stok.ca/~mike/

The "`Stok' disclaimers" apply.
 
D

Dan Q

I would add that you can use backticks to execute a shell command, as follo=
ws:
`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

Hello All,

I am starting to experiment with Ruby for Sys Admin tasks. =A0Now, I hav= e
been working with Shell scripting for a few years now. =A0Currently, I a= m
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" \
=A0 =A0 =A0 =A0 =A0 =A0 =A0"Bank File" \
=A0 =A0 =A0 =A0 =A0 =A0 =A0"Study Case" \
=A0 =A0 =A0 =A0 =A0 =A0 =A0"Overall Status" ;
do
=A0 =A0 =A0 =A0 =A0 =A0 =A0scp $groups user@server1:/tmp
done


For Ruby, I tried -

for group in Account File",
=A0 =A0 =A0 =A0 =A0 =A0 =A0"Bank File",
=A0 =A0 =A0 =A0 =A0 =A0 =A0"Study Case",
=A0 =A0 =A0 =A0 =A0 =A0 =A0"Overall Status"
do
=A0 =A0 =A0 =A0 =A0 =A0 =A0(scp groups user@server1:/tmp)
end

Of course, I get syntax errors everywhere. =A0I am not sure how to use a
for loop in this case. =A0Any input?


[ "Account File", "Bank File", "Study Case", "Overall Status" ].each do |= group|
=A0puts "processing #{group}"
end

Hope this helps,

Mike

--

Mike Stok <[email protected]>
http://www.stok.ca/~mike/

The "`Stok' disclaimers" apply.
 
J

Josh Cheek

[Note: parts of this message were removed to make it a legal post.]

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

Unix4Linux Unix4Linux

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

Unix4Linux Unix4Linux

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

Robert Klemme

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
 
U

Unix4Linux Unix4Linux

Robert said:
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
 
G

Gary Wright

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.

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,144
Latest member
KetoBaseReviews
Top