I am currently trying to make a bash script that will go into every folder in ./ and compile blah.java, then run blah sending in a number for an input, and put the output in a file that I have named. I have something that is somewhat functional, but it only goes into the first directory, and after that it fails on me. I currently have…
#! /bin/bash
for i in $(find . -maxdepth 1 -type d)
do
pwd
cd $i
pwd
if [ -f "blah.java" ];
then
javac -cp . blah.java
echo "17" | java -cp . blah - > blahresult17
echo "43" | java -cp . blah - > blahresult43
fi
done
I think it is having problems, because it is trying to go into a directory from ./ once it goes into a subdirectory, but obviously from the subdirectory it doesn’t exist.
You’re missing a fi on your if, but that’s presumably a cut and paste error, not the source of your failure. The likely problem is that once you do the first cd, the following cd commands are relative to where you were, not where you are now. You can work around this with pushd/popd instead, or running the cd and java bits in a subshell so that the cd does not persist to the next loop iteration.