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 }'
[ ... ]
(single brackets) somewhere that is generating the error. Inbash
, the[[ ... ]]
test doesn't generate that error when using-gt
with non-integers.[[ ... -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.if [[ "$((systemctl status cassandra | awk '/(Active: failed)/{print $10}') | awk '{print $0+0}')" -gt "${threshold}" ]]; then echo "greater than ${threshold}"