Bash script - two for loops with array problem...
Hello!
I am trying to execute a .c program of mine through bash... Problem is it takes two arguments (files) and am trying to use "for" loops to do it. I use the for loops to take the files (same number in both directories) and put them into arrays. I use the command "ls -1" in the "for loop" so they are sorted in such a way that they are in the same time and date order upon when the program is executed. Here is the code: Code:
for a in `ls -1 /analyses/data1/*_prt.txt`; So, I then tried: Code:
for a in `ls -1 /analyses/data1/*_prt.txt`; Any ideas how to do this? Just need to run the program with the two arrays and have them both advance together. |
Modify the second case so that also the outer loop uses a do and done.
BTW: `ls ...` isn’t necessary. A plain /analyses/data1/*_prt.txt will also do it. |
Such as:
Code:
for a in `ls -1 /analyses/data1/*_prt.txt`; Only problem I worry about is that won't it increase the outer loop and then increase the inner loop unevenly? I mean it would execute it like: ./plot_data $a[0] $b[0] >> log_file.log; ./plot_data $a[0] $b[1] >> log_file.log; ./plot_data $a[0] $b[2] >> log_file.log; ... and so on? As I want it to be like: ./plot_data $a[0] $b[0] >> log_file.log; ./plot_data $a[1] $b[1] >> log_file.log; ./plot_data $a[2] $b[2] >> log_file.log; ... and so on. That make sense (using [] to represent the elements in the array, starting at 0). |
Aha, I thought you want to get all possible combinations. When it should be only one round, you can replace the path in the statement:
Code:
for FIRST in /analyses/data1/*_prt.txt; do Code:
FIRST=(/analyses/data1/*_prt.txt) |
Quote:
I guess though, what does: #FIRST[*] mean? I am guessing the number of elements in the array? total? |
Quote:
When they have different names: do they have the same order? I mean: you might end up with different and wrong combinations otherwise. |
Quote:
blend=(/data3/etownsend/analyses/blend/cases_reran/ppaes_join_200*_prt.txt) like: blend=(ls -1 /data3/etownsend/analyses/blend/cases_reran/ppaes_join_200*_prt.txt) ? |
Quote:
Actully, both [i] are getting passed in... |
You won't need it I think. `ls -1 ...` will list in one column on screen. But inside any assignment, it’s always used in this way by default.
NB: I forgot the curly braces in the call’s arguments: $FIRST[i] => ${FIRST[i]} |
Code:
for ((i=0;i<${#FIRST[*]};i++)); do Code:
for i in ${!FIRST[*]}; do As for using ls to set an array, you need do this: Code:
It's still not advisable nor necessary to use ls though. If the files are there, then simple globbing should generate exactly the same list as ls does. |
Quote:
Thus my script looks like: [CODE] FIRST=(/data1/analyses/*_prt.txt) SECOND=(/data2/analyses/*_ht.txt) echo ${#FIRST[*]} " elements in the data array" > log.log echo ${#SECOND[*]} " elements in the data array" >> log.log for ((i=0;i<${#BLEND[*]};i++)); do ./program ${FIRST[i]} ${SECOND[i]} echo " " >> blend_images_reran.log echo "--------------" >> blend_images_reran.log echo " " >> blend_images_reran.log done |
Quote:
Does that mean the for loops knows the filenames in the array? |
Why not try echoing it out and see for yourself? ;)
But if you insist: Code:
$ array=( Fry Bender Zapp Leela ) |
Yeah, I should have X_X... Don't know why I didn't.
Thanks though, props for the futurama reference in the example. |
All times are GMT -5. The time now is 04:04 PM. |