Dear all
I'm a bash newbie trying to do the following. I currently have a folder called "100-mT", which contains several subfolders. I am looking for a way to perform the following steps in each iteration in a for loop (for idx in 200 300 400 500 600 700 800 900 1000) in the bash environment:
(i) Copy the folder "100-mT", and rename it to "idx-mT" (I would also be interested in knowing how, when idx=1000, the folder name becomes "1-T", but it is secondary)
(ii) Search in each of the subfolders that make up the new folder "idx-mT" for a file called "input", and replace in the text string "sim:applied-field-strength = 100 !mT" with "sim:applied-field-strength = idx !mT" (I would be interested in replacing the aforementioned line, when idx=1000 by "sim:applied-field-strength = 1 !T")
As information that may be useful, the folder "100-mT" has the following subfolders (each new line implies that the folders cited are within the above):
(i) "Rectangular-Sample", "Square-Sample"
(ii) "0-K", "2-K"
(iii) "Dipolar-Hierarchical", "Dipolar-Tensorial"
(iv) "Scaled-DMI", "Unscaled-DMI"
(v) "D3-D1-1", "D3-D1-1with2", "D3-D1-1with4", "D3-D1-1with6", "D3-D1-1with8", "D3-D1-2", "D3-D1-2with2", "D3-D1-2with4", "D3-D1-2with6", "D3-D1-2with8", "D3-D1-3"
Within these last folders are the "input" files that I referred to, which have different lines, line 28 being the aforementioned text entry "sim:applied-field-strength = 100 !mT".
My attempt at the moment is as follows
for idx in 200 300 400 500 600 700 800 900 1000;
do
# Let's copy, paste, and rename the relevant folder
if "$idx"<1000;
then
cp -r 100-mT "$idx"-mT;
echo "$idx-mT";
cd "$idx-mT";
else
cp -r 100-mT 1-T;
cd "1-T";
fi
# Let's create and loop over all subfolders
for Geometry in Rectangular-Sample Square-Sample;
do
echo "$Geometry";
cd "$Geometry";
for Temperature in 0-K 2-K;
do
echo "$Temperature";
cd "$Temperature";
for Dipolar in Dipolar-Hierarchical Dipolar-Tensorial;
do
echo "$Dipolar";
cd "$Dipolar";
for DMI in Scaled-DMI Unscaled-DMI;
do
echo "$DMI";
cd "$DMI";
for DMI_Value in D3-D1-1 D3-D1-1with2 D3-D1-1with4 D3-D1-1with6 D3-D1-1with8 D3-D1-2 D3-D1-2with2 D3-D1-2with4 D3-D1-2with6 D3-D1-2with8 D3-D1-3;
do
echo "$DMI_Value";
cd "$DMI_Value";
# Let's replace the numerical value of the relevant text string
if "$idx"<1000;
then
sed 's/sim:applied-field-strength = 100 !mT/sim:applied-field-strength = "$idx" !mT' <input >input;
else
sed 's/sim:applied-field-strength = 100 !mT/sim:applied-field-strength = 1 !T' <input >input;
fi
cd ..
done
cd ..
done
cd ..
done
cd ..
done
cd ..
done
cd ..
done
It seems that this approach it is not able to create the "$idx-mT" folders when $idx is lower than 1000. It creates correctly the "1-T" folder, but it does not change the relevant line on the input files thereon.
Any ideas?