# For Loop

**URL:** <https://www.ruby-forum.com/t/for-loop/179219>\
**Category:** Ruby\
**Created:** [November 19, 2009, 9:38pm UTC](https://www.ruby-forum.com/t/for-loop/179219 "2009-11-19T21:38:47Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![unix4linux](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/c57346/32.png) [@unix4linux](https://www.ruby-forum.com/u/unix4linux)\
**Post date:** [November 19, 2009, 9:38pm UTC](https://www.ruby-forum.com/t/for-loop/179219/1 "2009-11-19T21:38:47Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![unix4linux](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/c57346/32.png) [@unix4linux](https://www.ruby-forum.com/u/unix4linux)\
**Post date:** [November 19, 2009, 9:53pm UTC](https://www.ruby-forum.com/t/for-loop/179219/2 "2009-11-19T21:53:05Z")

</div>

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. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
[http://www.stok.ca/~mike/](http://www.stok.ca/~mike/)

The “`Stok’ disclaimers” apply.

---

<div class="post-metadata">

**Author:** ![unix4linux](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/c57346/32.png) [@unix4linux](https://www.ruby-forum.com/u/unix4linux)\
**Post date:** [November 19, 2009, 9:56pm UTC](https://www.ruby-forum.com/t/for-loop/179219/3 "2009-11-19T21:56:36Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![unix4linux](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/c57346/32.png) [@unix4linux](https://www.ruby-forum.com/u/unix4linux)\
**Post date:** [November 19, 2009, 10:15pm UTC](https://www.ruby-forum.com/t/for-loop/179219/4 "2009-11-19T22:15:20Z")

</div>

On Thu, Nov 19, 2009 at 2:52 PM, Mike S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

---

<div class="post-metadata">

**Author:** ![unix4linux](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/c57346/32.png) [@unix4linux](https://www.ruby-forum.com/u/unix4linux)\
**Post date:** [November 19, 2009, 10:33pm UTC](https://www.ruby-forum.com/t/for-loop/179219/5 "2009-11-19T22:33:54Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![unix4linux](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/c57346/32.png) [@unix4linux](https://www.ruby-forum.com/u/unix4linux)\
**Post date:** [November 19, 2009, 10:41pm UTC](https://www.ruby-forum.com/t/for-loop/179219/6 "2009-11-19T22:41:02Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![unix4linux](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/c57346/32.png) [@unix4linux](https://www.ruby-forum.com/u/unix4linux)\
**Post date:** [November 20, 2009, 1:42pm UTC](https://www.ruby-forum.com/t/for-loop/179219/7 "2009-11-20T13:42:28Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![unix4linux](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/c57346/32.png) [@unix4linux](https://www.ruby-forum.com/u/unix4linux)\
**Post date:** [November 20, 2009, 8:42am UTC](https://www.ruby-forum.com/t/for-loop/179219/8 "2009-11-20T08:42:44Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![unix4linux](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/c57346/32.png) [@unix4linux](https://www.ruby-forum.com/u/unix4linux)\
**Post date:** [November 20, 2009, 5:04pm UTC](https://www.ruby-forum.com/t/for-loop/179219/9 "2009-11-20T17:04:53Z")

</div>

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.
