1

I have a set of files in a directory

test1.in
test2.in
test3.in
test4.in
...

I have written a conversion tool to extract some data from those files and save it into another file.

I want to run that conversion tool for each file and save the results into different output files

./tool -i test1.in -i test1.out
./tool -i test2.in -o test2.out

I have seen a lot of answer to iterate on the files and run the desired command for each file, but I am not sure how to specify the output.

for file in dir/*.in; do
    ./tool -i file -o ???
done

How can I create a name for the output file?

jjcasmar
  • 1,387
  • 1
  • 18
  • 30

1 Answers1

1
for file in dir/*.in; do
    ./tool -i "$file" -o "${file%.*}".out
done

"${file%.*}" represents the value of $file before the last . (See https://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html#Shell-Parameter-Expansion, look for ${parameter%word}).

pii_ke
  • 2,811
  • 2
  • 20
  • 30