Reputation: 284
I am trying to create 2 folders and some files inside them. But it can't create more than the 1st folder and the 1st file. Code says it can't create the 1st folder since it is exist. Don't even try to create the rest of the files and folders.
Here is what I tried
#!/bin/bash
declare -a arrRel=(rel20 rel21)
declare -a arrVar=(pt_el pt_mu)
declare -a arrVarTitle=("electron p_T" "muon p_T")
for i in "${arrRel[@]}"
do
mkdir "${arrRel[$i]}"
cd "${arrRel[$i]}"
for j in "${arrVar[$j]}"
do
textFile=text_${arrRel[$i]}_${arrVar[$j]}.txt
targetDir=Desktop/samples
cat >${textFile} <<EOF
"some tex"
EOF
done #arrVar
cd ../ #cd arrRel
done #for loop over releases
To sum up, there should be 2 folders, rel20 and rel21 and two text files in both. But I just get the folder rel20 and just one text file in it.
I'd appreciate if you can point me why this doesn't work.
Upvotes: 0
Views: 183
Reputation: 776
I think from what you posted, that his is what you are looking for.
#!/bin/bash
declare -a arrRel=(rel20 rel21)
declare -a arrVar=(pt_el pt_mu)
declare -a arrVarTitle=("electron p_T" "muon p_T")
for i in "${arrRel[@]}"
do
mkdir "$i"
cd "$i"
for j in "${arrVar[@]}"
do
textFile=text_$i_$j.txt
targetDir=Desktop/samples
cat >${textFile} <<EOF
"some tex"
EOF
done #arrVar
cd ../ #cd arrRel
done
Not sure what your intent for arrVarTitle is.
Upvotes: 2
Reputation: 212298
You're indexing the arrays incorrectly. Frankly, the arrays are adding no value, and they're not worth the confusion. Just do:
#!/bin/bash
for i in rel20 rel21; do
( # This open paren is important
mkdir -p $i
cd $i
for j in pt_el pt_mu; do
textFile=text_$i_$j.txt
targetDir=Desktop/samples
cat >${textFile} <<-EOF
"some tex"
EOF
done
) # end subshell to recover previous working directory
done
Upvotes: 2