0

I created a simple script in bash to start system service if system is down for more than threshold value.If part basically check the integer value(extracted from the commnd)with threshold value(integer)

control is not going to if statement. can anyone suggest me the solution for above. how to test integer expression in IF statement.

#!/bin/bash
**function start_repair {                                                       
        threshold=120
        IP=10.194.51.216
        echo "$((systemctl status cassandra | awk'/(Active:failed)/{print$10}')| awk '{print ($0+0)}')"
        if [[ "$((systemctl status cassandra | awk '/(Active: failed)/{print $10}') | awk '{print ($0+0)}')" -gt "${threshold}" ]]; then
          echo "greater than ${threshold}"
          service cassandra restart
          PID=$!
          wait $PID
          echo done
    fi }**

ERROR: bash: line 5: [: : integer expression expected

  • The error does not come from the piece of code that you show. You have another [ ... ] (single brackets) somewhere that is generating the error. In bash, the [[ ... ]] test doesn't generate that error when using -gt with non-integers. – Kusalananda Apr 24 '18 at 7:39
  • but when i try to print the same test part, it's giving me correct output as integer. echo "$((systemctl status cassandra | awk'/(Active:failed)/{print$10}')| awk '{print ($0+0)}')" – Parul Chourasia Apr 24 '18 at 7:47
  • Sure, but the [[ ... -gt ... ]] test will not generate that error, so my guess is that your error is in another part of your code where you use [ ... ] to test an integer. Also, PID in your function will not be what you think it will be as there is no background job started. – Kusalananda Apr 24 '18 at 7:57
  • I removed ()bracket from print{} block in above lines. Now it's working. but i still have doubt why it created the problem. if [[ "$((systemctl status cassandra | awk '/(Active: failed)/{print $10}') | awk '{print $0+0}')" -gt "${threshold}" ]]; then echo "greater than ${threshold}" – Parul Chourasia Apr 24 '18 at 8:15
  • moreover i changed -gt to > – Parul Chourasia Apr 24 '18 at 8:32
0

There are a few issues with your question and with your function.

First of all, the [: : integer expression expected error is not generated by your function. The [[ ... -gt ... ]] test in the function does not generate this error (test it on the command line with [[ "hello" -gt "world" ]]). You have another test somewhere, using single brackets, that generates this error (test this on the command line with [ "hello" -gt "world" ]). Look for a [ ... ] arithmetic test elsewhere in your code.

You can see this in the error message itself since bash tells you that it's the [ utility that produces an error and that the thing that it tried to do an arithmetic test on is an empty string.

You said in comments that changing print (0+$0) to print 0+$0 in your awk code made it work. I doubt that this change made any difference whatsoever as the statements are equivalent. It's more likely that the input to the script changed in such a way that the error was not triggered.

You then commented that you changed -gt to > in the test. This changes the test to a lexicographical string ordering test. Such a test would be true if the left hand side string would be ordered after the right hand side string, so, for example, 20 > 100 would be true.

This is also missing the point that the error is not actually in the function that you are presenting.

My suggestion is to write your script and test each component of the script as you're writing it, so that you know that every single addition to the script does what you want them to do.


As for the function itself, it has a number of issues.

For example, the PID variable is probably supposed to hold the process ID of the cassandra process, but since you don't start cassandra as a background job (using & at the end of the command), PID will not be what you think it ought to be, and the subsequent wait $PID will not wait for the process you think it's waiting for.

It's unclear what you want to achieve with the PID variable and the wait call. My guess is that you can just remove the assignment to PID and the call to wait.

The $(( ... ) | ... ) is better written as $( ... | ... ). Firstly, running systemctl | awk in a subshell is unnecessary, and secondly, it's confusing to read because $(( ... )) is an arithmetic evaluation.

The code

awk'/(Active:failed)/ {print $10}' | awk '{print ($0+0)}'

may be combined into

awk'/(Active:failed)/ { print 0+$10 }'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.