0

I have created a script in bash. I need to check if take 0 number then ignore this, but if take some value then I need to calculate based on the value given and print the result.

Here is an example:

if [ "$max_users_conn" -ne "0" ]; then
  let "percentage="$process_list"*100/"$max_users_conn""
  echo "$percentage"      
fi

However, when I run the code, I receive the following error:

./mysql_conn.sh: line 14: [: 0 10: integer expression expected ./mysql_conn.sh: line 14: [: 0 10: integer expression expected ./mysql_conn.sh: line 14: [: 5 5: integer expression expected 0 20

I also tried to run it like this:

if [ "$max_users_conn" != "0" ]; then
  let "percentage="$process_list"*100/"$max_users_conn""
  echo "$percentage"      
fi

But this gives a different error:

./mysql_conn.sh: line 15: let: percentage=4*100/0: division by 0 (error token is "0") ./mysql_conn.sh: line 15: let: percentage=4*100/0: division by 0 (error token is "0") 0 0 20

Qantas 94 Heavy
  • 15,750
  • 31
  • 68
  • 83
Vasia SowSow
  • 31
  • 1
  • 6
  • possible duplicate of [Integer expression expected error in shell script](http://stackoverflow.com/questions/19505227/integer-expression-expected-error-in-shell-script) – arco444 Jun 08 '15 at 12:16
  • i try to do from this link, if [[ "$max_users_conn" -ne 0 ]]; then, but have the same error – Vasia SowSow Jun 08 '15 at 12:18
  • I have checked with max_users_conn=5 if [ "$max_users_conn" -ne "0" ]; then process_list=10 let "percentage="$process_list"*100/"$max_users_conn"" echo "$percentage" fi and it worked with no problem. Did you try your script on different computer ? – irukeru Jun 08 '15 at 12:21

2 Answers2

0

In BASH you can do:

if [[ $max_users_conn -ne 0 ]]; then
  percentage=$((process_list * 100 / max_users_conn))
  echo "$percentage"      
fi

but keep in mind that BASH doesn't support floating point maths and you will get an integer value returned.


To get decimal number returned use this bc command above.

percentage=$(bc -l <<< "scale=2; $process_list * 100 / max_users_conn")
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • 1
    If you need floating point arithmetic in a shell, zsh supports it (to a limited extent). – cmh Jun 08 '15 at 12:21
  • 1
    (1) you don't need quotes inside `[[ ... ]]` (2) `!=` is a string comparison, you want `-eq` instead. See [here](http://mywiki.wooledge.org/BashFAQ/031) for more details. – lcd047 Jun 08 '15 at 15:24
  • I think it should be `-ne` instead of `-eq` – anubhava Jun 08 '15 at 15:30
0

Bash does not allow you math expressions like other languages do, you need to use the $(( ... )) operator for this. However, this operator gives you only operations base on integers if you supply integers: echo $((2/4)) # => 0. If you do not care about fractions, though, it will help you. If you need fractions, utilize bc like anubhava showed in his answer.

Additionally, you should check that $max_users_conn actually contains a value.

So, rewrite your snipped to look like this:

if [ -n "$max_users_conn" -a "$max_users_conn" -ne 0 ]; then
    percentage=$(( $process_list * 100 / $max_users_conn))
    echo "$percentage"      
fi

test "$a" -ne 0 checks for numeric values, while test "$a" != "$b" checks for string equivalence.

Technaton
  • 944
  • 4
  • 15
  • have the next : ./mysql_conn.sh: line 14: [: 0 10: integer expression expected ./mysql_conn.sh: line 14: [: 0 10: integer expression expected ./mysql_conn.sh: line 14: [: 5 5: integer expression expected ./mysql_conn.sh: line 15: 0 * 100.0 / 5: syntax error: invalid arithmetic operator (error token is ".0 / 5") – Vasia SowSow Jun 08 '15 at 12:28
  • Does `$max_users_conn` actually have a value? Have you checked? What about: `[ -n "$max_users_conn" -a "$max_users_conn" -ne 0 ]`? Additionally, my $(( )) operator contained a typo (100.0) from zsh, bash doesn't allow "100.0" but just "100". I corrected my answer accordingly. – Technaton Jun 08 '15 at 12:37
  • Oh, does `$max_users_conn` contain a numeral at all? – Technaton Jun 08 '15 at 12:44