Home > Mobile >  How do I create a file in a for loop that includes the variable and added text?
How do I create a file in a for loop that includes the variable and added text?

Time:03-09

I have the following code, where I would like to create directories that are named by the variable number and then _DOS is added to the filename. (Basically I want the code to create directories named: 1_DOS 2_DOS 3_DOS, and then save the files POSCAR and log inside the created directory.) However, when I run the job, it is not creating the desired directories. I think my error is in mkdir $i_DOS. I think the code is looking for i = i_DOS and not finding it. When I run this, the error returns as mkdir: missing operand Do you have any suggestions?

 for i in 1 2 3
 do
 mkdir $i_DOS
 cp POSCAR $i_DOS
 cp log $i_DOS
 done
 exit

Thank you in advance.

CodePudding user response:

I think you are supposed to use !i! or %i% and not $i as it references a label.

 for i in 1 2 3
 do
 mkdir !i!_DOS
 cp POSCAR !i!_DOS
 cp log !i!_DOS
 done
 exit

CodePudding user response:

use {} braces around the variable i to resolve ambiguity.

 for i in 1 2 3
 do
 mkdir ${i}_DOS
 cp POSCAR ${i}_DOS
 cp log ${i}_DOS
 done
 exit
  • Related