Monday, April 11, 2022

[SOLVED] I want to use shell scripting to run my java program for each of my input files

Issue

I have a directory of input files and I want to use a shell script to execute these files onto the console and then create output files in a new output directory.

#!/bin/bash

FILES="inputs/*.txt"
for f in $FILES
do
    basename=${f%.*}
    java main accounts.txt rentalunits.txt
    cat "$f"
    
done >> $basename.out

I tried running this code using

sh testscript

for two text files but it only creates one output file and gets stuck at the first scanner in the java program.


Solution

Just guessing

#!/bin/bash

FILES="inputs/*.txt"
for f in $FILES
do
    basename=${f%.*}
    java main accounts.txt rentalunits.txt < "$f" > "$basename.out"
done

Also, no need to specify the interpreter as you have the shebang

chmod +x testscript
./testcript


Answered By - Diego Torres Milano
Answer Checked By - Gilberto Lyons (WPSolving Admin)