1

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

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I have tried dos2unix filename filename and it doesn't work out too. But with what you've suggested, it's working well. Thanks!
A single \r is a mac line break. You should have used mac2unix.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.