Question: Bash script iterating over 2 arrays
1
gravatar for mdriskil
7 months ago by
mdriskil10
mdriskil10 wrote:

Hello all,

I was wondering if I could get some help with a script that I have been struggling with. I am trying to iterate over two directories with a for loop, but I also need to use the names of the files from one of my directories to name the new files. I can't get the variable name[$i] to work. Each directory has the same amount of files, which is 20 files. So, when I "echo ${name[$i]}" is should get 20 strings outputted, but I get 1.

Here is my code:

cd /Users/personal/Desktop/blackberry_hybseq_data/Data_and_Nov2018_NZ_assembly/blackberry_hybseq/Align/Hillquist/

array=ls -1 /Users/personal/Desktop/blackberry_hybseq_data/Data_and_Nov2018_NZ_assembly/blackberry_hybseq/*gz array_2=ls -1 /Users/personal/Desktop/blackberry_hybseq_data/Data_and_Nov2018_NZ_assembly/blackberry_hybseq/Align/Hillquist/*.bam

for ((i=0; i<${#array[@]}; ++i)); do

name[$i]=echo ${array[$i]} | cut -d "/" -f8 | cut -d "." -f1;

echo ${name[$i]};

done exit

Here is my output:

R01_ccs.UV3pbc017__UV3pbc017

Any help on how to get my code to fully iterate over all 20 entries and not just the first one, would be greatly appreciated.

ADD COMMENTlink modified 7 months ago • written 7 months ago by mdriskil10
0
gravatar for dariober
7 months ago by
dariober10k
WCIP | Glasgow | UK
dariober10k wrote:

I don't know exactly what's happening with your command but I think to create the arrays you can use something like

array=(`ls -1 /Users/personal/Desktop/blackberry_hybseq_data/Data_and_Nov2018_NZ_assembly/blackberry_hybseq/*gz`)

Then iterate with:

for ((i=0; i<${#array[@]}; ++i));
do
    name[$i]=`echo ${array[$i]} | cut -d "/" -f8 | cut -d "." -f1`
    echo ${name[$i]};
done

(Note the backticks characters)

But maybe there is a better way altogether to rename your files since your strategy of iterating in parallel two lists of files could lead to wrong renaming if the two lists are not correctly paired...

ADD COMMENTlink written 7 months ago by dariober10k
0
gravatar for mdriskil
7 months ago by
mdriskil10
mdriskil10 wrote:

Hi,

Thanks for the help. Yeah, I have the tilde around my code when making a variable like you have. It didn't show in my code I posted above. hmm I will have to figure that out. This is a test code that I created to use in a bigger script that I will need to pull files from two different directories and I will have to name the output files to reflect the same names as the files in one of the directories. The order of the files are the same for both directories.

My main problem is echo ${name[$[i]} is only outputting one iteration. It just outputs R01_ccs.UV3pbc017__UV3pbc017. When it should be outputting 20 values because there are 20 files in that directory.

I hope that explains my problem better. :) Again, thanks for the help.

ADD COMMENTlink modified 7 months ago • written 7 months ago by mdriskil10
Please log in to add an answer.

Help
Access

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 2024 users visited in the last hour