Reputation: 1805
I have this bash script on my CentOS 5.3.
#!/bin/bash RESULT=$(cat /tmp/logfile.log | grep -i "Backlog File Count" | awk '{print $6}') if [ "${RESULT}" -lt "5" ]; then echo "${RESULT} is less than 5" else echo "${RESULT} is greater than 5" fi
/tmp/logfile.log:
Backlog File Names (first 1 files)
This bash script should supposedly to get the value "1" on the log file and print the output. However, when I run the script, this is the error message:
: integer expression expected
So when I set the debug mode, I found the "RESULT" variable output:
+ RESULT=$'1\r' ..... + '[' $'1\r' -lt 5 ']'
I've noticed that "\r" output is attached on the value.
I would appreciate if any one could lead me why there is "\r" on that output and how to get rid of that error. I tried on CentOS 6.3 and there is no issue.
# rpm -qa bash bash-3.2-32.el5_9.1
Thank you. James
Upvotes: 1
Views: 12049
Reputation: 123448
Your input file contains CR+LF line endings. Strip the CR before reading the variable. Say:
RESULT=$(tr -d '\r' < /tmp/logfile.log | grep -i "Backlog File Count" | awk '{print $6}')
Alternatively, you could remove the carriage returns from the input file by using dos2unix
or any other utility.
Moreover, saying:
cat file | grep foobar
is equivalent to saying:
grep foobar file
and avoids the Useless Use of Cat.
Upvotes: 4