Home > Software design >  Looping through file 001 to 909 in Bash
Looping through file 001 to 909 in Bash

Time:03-23

Suppose I have one python script which takes in two arguments:

python3 a.py '/001/001.txt' '/after/001'

'/001/001.txt' would be the input file path, '/after/001' would be the output path, I would like to know how can I set the input_path and output_path in the following bash commands to loop automatically through file 001 to 909.

i=1; 
while [ $i -le 909 ]; 
do 
    input_path = ''
    output_path = '';
    python3 a.py $input_path $output_path
 
    i=$((i 1)); 
done

I am first time using bash commands, so can anyone share how to solve this problem? Thank you so much

CodePudding user response:

With bash version >= 4.0:

for i in {001..909}; do
  echo python3 a.py "/$i/$i.txt" "/after/$i"
done

With bash version >= 3.1:

for ((i=1; i<=909; i  )); do
  printf -v x "d" "$i"
  echo python3 a.py "/$x/$x.txt" "/after/$x"
done

If output looks okay, remove echo.

  • Related