0

I am running a genetic algorithm for multiple seed values and for each run I want to put the seed value in the name of the output file. I have been able to do that using the following Bash script.

  1 #!/bin/bash
  2 # Multi-seed script
  3 
  4 seed=0.01
  5 for count in {1..100..1} 
  6   do echo "$seed"
  7   ./nsga2r $seed < input_data/truss.in
  8   $(mv all_pop.out all_pop_seed"$count".out)
  9   seed=$(echo "$seed+0.01" | bc)
 10 done

The script if very straight forward. Line-5 runs the loop for 100 seed values. Line-7 runs the program nsga2r for current seed value taking input values from file input_data/truss.in. Line-8 adds the current seed value to the name of the output file of current seed value. This yields the output as :

all_pop_seed1.out
all_pop_seed2.out
all_pop_seed3.out
... 
all_pop_seed10.out
all_pop_seed11.out
...
all_pop_seed99.out
all_pop_seed100.out

BUT how do I name them like the following instead?

all_pop_seed001.out
all_pop_seed002.out
all_pop_seed003.out
... 
all_pop_seed010.out
all_pop_seed011.out
...
all_pop_seed099.out
all_pop_seed100.out

Although this minor difference from my desired output filenames does not hinder my further analysis, still I would like to know.

Abhinav
  • 1,882
  • 1
  • 18
  • 34
  • so you want to be able to format `3` as `003`. do you see how you can formulate the question in a single line? reduce the problem to its minimal form. – Karoly Horvath Aug 08 '14 at 16:07

2 Answers2

1

See How to zero pad a sequence of integers in bash so that all have the same width?

printf "%03d" 99 

will produce 099 for example

Community
  • 1
  • 1
tolanj
  • 3,651
  • 16
  • 30
0

Use printf to format the file name. Also, you don't need the command substitution to execute the mv command.

#!/bin/bash
# Multi-seed script

seed=0.01
for count in {1..100..1} ; do
  echo "$seed"
  ./nsga2r $seed < input_data/truss.in
  printf -v fname "all_pop_seed%03d.out" "$count"
  mv all_pop.out "$fname"
  seed=$(echo "$seed+0.01" | bc)
done
chepner
  • 497,756
  • 71
  • 530
  • 681