1

I'm getting into the error for bash shell [: : integer expression expected" while running below script.

#!/bin/bash
sm=$(ps -e | grep sendmail > /dev/null 2>&1)
pm=$(/etc/init.d/postfix status > /dev/null 2>&1)
check_mail(){
if [ "$sm" -eq 0 ]; then
echo "Service Status: Sendmail is Running!"
elif [ "$pm" -eq 0 ]; then
echo "Service Status: Postfix Service is Running!"
else
echo "Service Status: Both Sendmail & Postfix Service is Not Running On $(uname -n)"
fi
}
check_mail

While running the above script it's simply showing the output of else condition.

Service Status: Both Sendmail & Postfix Service is Not Running On host

Though, i have tested "==" rather "-eq" for comparison and [[]] but did not worked.

3
  • 1
    You send all the output to devnull, so there will be nothing in your vars. Commented Apr 8, 2017 at 14:23
  • @123, even with not redirecting output to /dev/null 2>&1 giving the integer error. Commented Apr 8, 2017 at 16:13
  • Because neither of those commands outputs an integer. Commented Apr 8, 2017 at 17:15

2 Answers 2

2

I assume you are trying to assess presence of sendmail in the process list. You should change your code to this :

#!/bin/bash
check_mail(){
if
  ps -e | grep sendmail > /dev/null 2>&1
then
  echo "Service Status: Sendmail is Running!"
elif
  /etc/init.d/postfix status > /dev/null 2>&1
then
  echo "Service Status: Postfix Service is Running!"
else
  echo "Service Status: Both Sendmail & Postfix Service is Not Running On $(uname -n)"
fi
}
check_mail

The reason your original code fails is that you capture the output of commands using command substitution $(). The standard output, that is, not the exit code. Then, when using the [ -eq ] test, which expects an integer argument (which your variable does not contain), it fails with the message you get. Since both tests always fail, the else clause is entered.

By putting the actual commands inside the conditions of the if statement, you are using the actual numerical return code (0 for true, non-zero for false), which is what you want here.

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

5 Comments

I tried the same code as well before but same error.
You cannot see the [: : integer expression expected error in the code I proposed, because there is no [ test in that code.
@Fred.....My bad.. You are correct , but in your code we are directly evaluating the condition from ps -e | grep service and not testing it via test conditions.. I tried the same with the test [] condition and that fails, Just updated the another code what i tried..
@Karn I am not sure I understand what you mean. Direct evaluation from the command is what you need, and getting rid of [ ] is what you should do. It is the solution, not the problem. The if statement does not need a test in the form of [ ] or [[ ]], the return code of any command will do the job too.
Both the answers are correct though, i'm accepting @Fred's answer since he posted first, However i voted both as well.
2

It seems to me that you are confusing the exit-status of a program with the output.var=$(command) will put the output of command in var. As said in 123's comment, because you redirect everything to /dev/null, there is no output, and therefore, sm and pm are empty.

If you want to see the exit status, use $?:

#!/bin/bash
typeset -i pm
typeset -i sm
ps -e | grep sendmail > /dev/null 2>&1
sm=$?
/etc/init.d/postfix status > /dev/null 2>&1
pm=$?

check_mail(){
    if [ $sm -eq 0 ]; then
        echo "Service Status: Sendmail is Running!"
    elif [ $pm -eq 0 ]; then
        echo "Service Status: Postfix Service is Running!"
    else
        echo "Service Status: Both Sendmail & Postfix Service is Not Running On $(uname -n)"
    fi
}
check_mail

3 Comments

@Ljm....I just update my post where i applied the test condition and even its not yielding the correct Status and producing the integer error as well.
Ah yes, remove the quotes. I missed that one. Full script in now in the answer.
in Bash typeset is deprecated.

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.