Bash – echo string >> file does not work

bashechoio-redirection

I wrote the following script:

for filename in `find . -name '*'.cpp | grep $IN_REGEX | grep -v $OUT_REGEX`
do
    echo "Output file is $OUTPUT_FILE"
    count=`git log --pretty=format: --name-only $filename | grep -v ^$ | wc -l`
    echo "$count    $filename" >> $OUTPUT_FILE
done

But nothing gets written into the output file.

Please note:

  1. I have set the values for OUTPUT_FILE, IN_REGEX and OUT_REGEX.
  2. The code inside the loop is being executed. I checked this with an sh -x invokation.
  3. When I remove the >> $OUTPUT_FILE I get the output.
  4. I tried a touch $OUTPUT_FILE inside the script and that is working fine.

Can someone please point out what is my mistake here?

Best Answer

Does $OUTPUT_FILE exist? What are its permissions (ls -l $OUTPUT_FILE, getfacl $OUTPUT_FILE)? How many times is the echo "Output file..." executed? (I'd put that one outside the for, but it's your call)?

(I'm suspecting your find ... pipeline doesn't return anything)

Related Question