0
#!/bin/bash
#Variable to store total word count
words=$(wc $1 | awk '{print $2}')
count=$(grep -c "$2" "$1")
echo "Word Count:" $words
echo "Instance count of "$2": "$count
percentage=$(expr $count / $words)
echo $percentage

My machine is running on Windows 10, this is my code, the script takes two arguments - a text file and a word.

words stores the total number of words from the text file, while count stores the instances of the word parameter in the text file.

echo $words returns 4 and echo $count returns 1 so why do I get the error: expr: non-integer argument

  • 2
    Your file has DOS line endings, and `words` and `counts` both actually end with carriage returns. The output of `echo "$words xxxxxxxxxx"`, for example, should be illuminating. – chepner Mar 20 '18 at 17:46
  • `declare -p words` would show that something is off, though not as clearly as it should. If you are using `bash` 4.4 or later, `echo "${words@Q}"` will clearly show the carriage return in the value. – chepner Mar 20 '18 at 17:48

0 Answers0