If I have two lists of variables: (note in the second list, the space is the element seperator)l1=(su1 su2 su3 su4)l2=(1,2,3 4,3,2 4,7,6 3,2,1)and I want to loop through both lists so that a command is executed for in the sense that su1 goes with 1,2,3, su2 goes with 4,3,2, su3 goes with 4,7,6 and su4 goes with 3,2,1So if each element of l1 corresponds to a directory and I want to do something like: #!/bin/bashdirectory=/some/pathl1=(su1 su2 su3 su4)l2=(1,2,3 4,3,2 4,7,6 3,2,1)for i in ${l1[@]}dofor e in ${l2[@]}docd $directory/$iecho ${e} > file.txtdonedoneIn other words, cd into each directory in l1 and make a file using the corresponding element in l2The above is what I've tried but it just makes a file using the first element of l2 in each directory in l1
looping through two lists of variables and assigning the corresponding order to commands
bash;shell script
Use this:# first create those directoriesmkdir ${l1[@]}# set counter value to 0c=0# loop trough the array l1 (while the counter $c is less than the length of the array $l1)while [ $c -lt ${#l1[@]} ]; do  # echo the corresponding value of array l2 to the file.txt in the directory  echo ${l2[$c]} > ${l1[$c]}/file.txt  # increment the counter  let c=c+1doneThe result:$ cat su1/file.txt 1,2,3$ cat su2/file.txt 4,3,2$ cat su3/file.txt 4,7,6$ cat su4/file.txt 3,2,1