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

前端 未结 5 2297
猫巷女王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:51

    Sorry for necromancing the thread, but whenever you iterate over files by globbing, it's good practice to avoid the corner case where the glob does not match (which makes the loop variable expand to the (un-matching) glob pattern string itself).

    For example:

    for filename in Data/*.txt; do
        [ -e "$filename" ] || continue
        # ... rest of the loop body
    done
    

    Reference: Bash Pitfalls

提交回复
热议问题