How to loop over files in directory and change path and add suffix to filename

前端 未结 5 2303
猫巷女王i
猫巷女王i 2020-11-22 06:23

I need to write a script that starts my program with different arguments, but I\'m new to Bash. I start my program with:

./MyProgram.exe Data/data1.txt [Logs/d

5条回答
  •  忘了有多久
    2020-11-22 06:56

    You can use finds null separated output option with read to iterate over directory structures safely.

    #!/bin/bash
    find . -type f -print0 | while IFS= read -r -d $'\0' file; 
      do echo "$file" ;
    done
    

    So for your case

    #!/bin/bash
    find . -maxdepth 1 -type f  -print0 | while IFS= read -r -d $'\0' file; do
      for ((i=0; i<=3; i++)); do
        ./MyProgram.exe "$file" 'Logs/'"`basename "$file"`""$i"'.txt'
      done
    done
    

    additionally

    #!/bin/bash
    while IFS= read -r -d $'\0' file; do
      for ((i=0; i<=3; i++)); do
        ./MyProgram.exe "$file" 'Logs/'"`basename "$file"`""$i"'.txt'
      done
    done < <(find . -maxdepth 1 -type f  -print0)
    

    will run the while loop in the current scope of the script ( process ) and allow the output of find to be used in setting variables if needed

提交回复
热议问题