0

To satisfy function requirements I have to retrieve a parameter which is a duty cycle (0-100% in 0.01%).

For test, I wrote something simple like :

#!/bin/bash
#

if [ "$1" -lt 0 ] || [ "$1" -gt 100 ]
then
    echo "bad param"
else
    echo "ok"
fi  

I obtain :

root@:~# ./test.sh 11
ok
root@:~# ./test.sh 11,01
./test.sh: line 4: [: 11,01: integer expression expected
./test.sh: line 4: [: 11,01: integer expression expected
bad param

How to realise this kind of test ?

3
  • 2
    I'm assuming that you're using the , as a decimal point? As your error is telling you, bash only supports integer numbers. Commented Jan 6, 2015 at 14:11
  • 1
    You will have to use bc (see stackoverflow.com/questions/12952427/… for instance) Commented Jan 6, 2015 at 14:12
  • @SnP: You can use awk for this. Commented Jan 6, 2015 at 14:27

1 Answer 1

2

bash can only operate in integer arithmetics. If you need floats, use an external tool like bc:

if (( $( bc <<< "($1 < 0) || ($1 > 100) " ) )) ; then
    ...
fi

I would rather switch to a more powerful language for the whole script, though (like Perl)/

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

2 Comments

Any reason for the == within [[, rather than using -eq or ((?
@TomFenech: Thanks, just a remain of the trial and error :)

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.