Skip to main content
added 340 characters in body
Source Link
cuonglm
  • 158.1k
  • 41
  • 342
  • 420

Your loadavg is null, causes syntax errors by [:

$ top -b n1 | grep -i load | awk -F, '{print$4}'| awk -F: '{print $2}'
<blank line here>

You must change it to:

$ top -b n1 | grep -i load | awk -F, '{print$4}'| awk -F: '{print $1}'
 0.24

However, you should use newer test in your script, it can handle this problem:

$ [[ "" -gt 1 ]] || echo "OK"
OK

With older [:

$ [ "" -gt 1 ] || echo "OK"
bash: [: : integer expression expected
OK

Updated

bash can not handle floating point numbers, so your comparison (even with new test [[..]]) will show errors.

You can use another tools to do this task, like bc, awk...

Example:

$ [[ $(echo "3 > 1" | bc -l) == 1 ]] && echo OK
OK
$[[ $(echo "3 < 1" | bc -l) == 1 ]] || echo Not OK
Not OK

Your loadavg is null, causes syntax errors by [:

$ top -b n1 | grep -i load | awk -F, '{print$4}'| awk -F: '{print $2}'
<blank line here>

You must change it to:

$ top -b n1 | grep -i load | awk -F, '{print$4}'| awk -F: '{print $1}'
 0.24

However, you should use newer test in your script, it can handle this problem:

$ [[ "" -gt 1 ]] || echo "OK"
OK

With older [:

$ [ "" -gt 1 ] || echo "OK"
bash: [: : integer expression expected
OK

Your loadavg is null, causes syntax errors by [:

$ top -b n1 | grep -i load | awk -F, '{print$4}'| awk -F: '{print $2}'
<blank line here>

You must change it to:

$ top -b n1 | grep -i load | awk -F, '{print$4}'| awk -F: '{print $1}'
 0.24

However, you should use newer test in your script, it can handle this problem:

$ [[ "" -gt 1 ]] || echo "OK"
OK

With older [:

$ [ "" -gt 1 ] || echo "OK"
bash: [: : integer expression expected
OK

Updated

bash can not handle floating point numbers, so your comparison (even with new test [[..]]) will show errors.

You can use another tools to do this task, like bc, awk...

Example:

$ [[ $(echo "3 > 1" | bc -l) == 1 ]] && echo OK
OK
$[[ $(echo "3 < 1" | bc -l) == 1 ]] || echo Not OK
Not OK
Source Link
cuonglm
  • 158.1k
  • 41
  • 342
  • 420

Your loadavg is null, causes syntax errors by [:

$ top -b n1 | grep -i load | awk -F, '{print$4}'| awk -F: '{print $2}'
<blank line here>

You must change it to:

$ top -b n1 | grep -i load | awk -F, '{print$4}'| awk -F: '{print $1}'
 0.24

However, you should use newer test in your script, it can handle this problem:

$ [[ "" -gt 1 ]] || echo "OK"
OK

With older [:

$ [ "" -gt 1 ] || echo "OK"
bash: [: : integer expression expected
OK