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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
07-03-2012, 03:39 PM
|
#1
|
LQ Newbie
Registered: Jul 2012
Posts: 5
Rep: 
|
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!!!!
|
|
|
07-03-2012, 04:55 PM
|
#2
|
Member
Registered: Jun 2012
Location: Canada
Distribution: Ubuntu/Debian/CentOS
Posts: 45
Rep:
|
I don't fully understand what you are trying to do, some more explanation would be very helpful.
|
|
|
07-03-2012, 05:07 PM
|
#3
|
LQ Newbie
Registered: Jul 2012
Posts: 5
Original Poster
Rep: 
|
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
|
|
|
07-03-2012, 05:14 PM
|
#4
|
LQ Guru
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573
|
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.
|
|
|
07-03-2012, 08:57 PM
|
#5
|
LQ Guru
Registered: Sep 2009
Location: Perth
Distribution: Arch
Posts: 10,037
|
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
|
|
|
07-04-2012, 11:55 AM
|
#6
|
LQ Newbie
Registered: Jul 2012
Posts: 5
Original Poster
Rep: 
|
Thank you so much, that worked perfectly.
|
|
|
07-05-2012, 12:30 AM
|
#7
|
LQ Guru
Registered: Sep 2009
Location: Perth
Distribution: Arch
Posts: 10,037
|
Please remember to mark as SOLVED once you have a solution.
|
|
|
07-05-2012, 01:41 AM
|
#8
|
LQ Newbie
Registered: Jul 2012
Posts: 3
Rep: 
|
It was just amazing information sharing and it's helpful for everyone.
|
|
|
All times are GMT -5. The time now is 06:54 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|