LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-03-2012, 04:39 PM   #1
JM_T
LQ Newbie
 
Registered: Jul 2012
Posts: 5

Rep: Reputation: Disabled
loops in scripts


I am trying to batch process some files. So far I have used only single files, using

for i in ax bx cx dx nx etc...
do
command1 (e.g. /../test$i)
command2 (e.g. /../test$i)
done

I have files that need to be "coupled".
e.g. ax with dx, bx with cx, nx with nnx, ....

all this is in a fairly random order and I have a table/matrix with the filenames coupled. They have to be processed together before going to the next one, and I can't figure out how to do it.
e.g. commands will look like
command1 ../../test/ax/dx
command2 ../../test/ax/dx

where the loop then should jump to
command1 ../../test/bx/cx
command2 ../../test/bx/cx

etc.

THANKS!!!!
 
Old 07-03-2012, 05:55 PM   #2
montel
Member
 
Registered: Jun 2012
Location: Canada
Distribution: Ubuntu/Debian/CentOS
Posts: 45

Rep: Reputation: 18
I don't fully understand what you are trying to do, some more explanation would be very helpful.
 
Old 07-03-2012, 06:07 PM   #3
JM_T
LQ Newbie
 
Registered: Jul 2012
Posts: 5

Original Poster
Rep: Reputation: Disabled
Basically I need to use 2 linked variables in one command line, repeat a few commands with this set of variables. Then next 2 variables need to go through the same set of commands are unrelated to the first 2, etc.
With "for i in..." I have been able to do this type of a loop with a single variable, but combining multiple sets of 2 linked variables within one loop I have not been able to figure out.

Hope that clarifies it better, not sure how better to put it. Thanks
 
Old 07-03-2012, 06:14 PM   #4
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,258

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
Like this?

Code:
#!/bin/bash

arr1=("ax" "bx" "nx")
arr2=("dx" "cx" "nnx")

for ((i=0; i<${#arr1[@]}; i++)); do
   echo command1 ../../test/${arr1[$i]}/${arr2[$i]}
   echo command2 ../../test/${arr1[$i]}/${arr2[$i]}
done
Output is
Code:
command1 ../../test/ax/dx
command2 ../../test/ax/dx
command1 ../../test/bx/cx
command2 ../../test/bx/cx
command1 ../../test/nx/nnx
command2 ../../test/nx/nnx

Just remove the echo from the lines to actually run the commands, rather than just printing out what it would run.
 
Old 07-03-2012, 09:57 PM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,250

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
Another take would be to use your file with the pairings in it. Assuming this looks like:
Code:
$ cat pairing_file
ax dx
bx cx
nx nnx
We can then read this in with a while loop like so:
Code:
while read -r first second
do
    command1 "../../test/$first/$second"
    command2 "../../test/$first/$second"
done<pairing_file
 
Old 07-04-2012, 12:55 PM   #6
JM_T
LQ Newbie
 
Registered: Jul 2012
Posts: 5

Original Poster
Rep: Reputation: Disabled
Thank you so much, that worked perfectly.
 
Old 07-05-2012, 01:30 AM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,250

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
Please remember to mark as SOLVED once you have a solution.
 
Old 07-05-2012, 02:41 AM   #8
kgntech
LQ Newbie
 
Registered: Jul 2012
Posts: 3

Rep: Reputation: 0
It was just amazing information sharing and it's helpful for everyone.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash Loops UnitedWeFall Programming 8 08-21-2007 07:33 AM
VPN: Debian Scripts -> Mandriva 2006 Scripts Undefined Mandriva 0 11-30-2005 01:10 PM
while loops + while : blizunt7 Linux - General 3 12-04-2004 06:27 PM
Indicating external argument into loops in SCRIPTs linsson Programming 7 07-28-2004 01:55 PM
loops JMK Linux - Newbie 11 04-09-2004 06:30 PM


All times are GMT -5. The time now is 01:07 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration