jslearner07 jslearner07 - 8 months ago 44
Linux Question

shell script while loop trigger build

I have a requirement to trigger a build for some specific projects, which are seperated in a text file with comma.

I wrote a shell script to split the contents of the file, extract the folder and navigate to that folder. Once navigation is done, trigger a build and this should happen for all project paths. The below script works fine if i do listing files in directory after navigation, but while doing a gradle build this executes only once.

Below is my shell script

[ ! -f $INPUT ] && { echo "$INPUT file not found"; exit 99; }
while read f1
echo "$f1 building...";
cd $f1;
gradle clean b u;
cd $CWD;

echo "Build over"

done < $INPUT

Any issue with this script? Please help

Answer Source

This is speculative (redirecting standard input for the gradle command), but I wanted to take the opportunity to fix some other problems with your script.

# Consider exiting with status 1 instead of 99 unless you have
# a specific reason for using 99.
[ ! -f "$INPUT" ] && { echo "$INPUT file not found"; exit 99; }
while IFS=, read -r f1 rest
  echo "$f1 building..."
  pushd "$f1"
  gradle clean b u < /dev/null

  echo "Build over"

done < "$INPUT"

I assume you are changing the value of IFS in order to split the line read from $INPUT; in that case, you need to give read at least two variables so that the first field is assigned to f1 and the rest of the line is assigned to another variable. Using pushd and popd together is simpler than manually recording the old working directory; since you were using a subshell, you didn't need to use CWD anyway because the effect of the first cd was only in effect until that subshell exited anyway.

If, for whatever reason, gradle does need access to standard input, use a different file descriptor to read from $INPUT.

while IFS=, read -r f1 rest <&3; do
  echo "$f1 building..."
  pushd "$f1"
  gradle clean b u
done 3< "$INPUT"