I have a bash script that is intended to be run from cron. The script counts the words in document, and appends a line with the current time and the word count to a CSV file. In relevant part, the script is:
EPOCH=$(date +"%s")
WCC=$(wc -w ~/path/filename.txt | sed 's/\/Users\/username\/path\/filename.txt//' | sed 's/ //g')
echo $EPOCH,$WCCH4 >> ~/path/wordcount-data.csv
This script is run from cron with the following entry:
0 * * * * sh /Users/username/path/wordcount.sh
The problem is that wc -w
returns a different value when run from cron than when run from the shell or when run from the shell script executed in the terminal. In other words, the value of this script executed from cron is currently 12438, but running wc -w filename.txt
in the shell or running sh ./wordcount.sh
both return values of 12445. For what it's worth the difference is always 7, and vim's word count matches the word count run from cron.
What accounts for the different values, and how do I fix it?