Bash Compare two variables, integer expression expected [duplicate] - bash

This question already has answers here:
Bash: Integer expression expected [duplicate]
(2 answers)
Closed 7 years ago.
I have a problem with comparing 2 variables (one variable is read from file).
1 #!/bin/bash
2
3 CFR=$(<bubble_status.txt)
4 treshold=0.01
5 echo $CFR
6 echo $treshold
7
8 if [ "$CFR" -gt "$treshold" ]
9 then
10 echo "CFR is greater then treshold";
11 elif [ "$CFR" -lt "$treshold" ]
12 then
13 echo "CFR is less than treshold";
14 else
15 echo "Something else";
16 fi
But bash returns me this:
19.81
0.01
./dupa7.sh: line 8: [: 19.81: integer expression expected
./dupa7.sh: line 11: [: 19.81: integer expression expected
Something else
Any ideas?

Bash doesn't support float comparison. You could use bc:
if (( $(bc <<< "$CFR > $threshold") )); then
echo "CFR is greater then threshold"
fi

Bash doesn't support floating point arithmetic. Use another tool such as bc or awk if you want to do floating point comparison/math.

Related

Why is bash behaving differently when it takes argument in braces? [duplicate]

This question already has answers here:
Brace expansion with a Bash variable - {0..$foo}
(5 answers)
Closed 1 year ago.
Input: range 6
function range {
echo {0..$1}
echo {0..6}
if [[ $1 =~ 6 ]]
then
echo "Equal"
fi
}
Output:
{0..6}
0 1 2 3 4 5 6
Why is the output different whereas $1 and 6 is equal?
The curly-braces get expanded before $-variables are expanded, and the curly-brace range syntax simply doesn't recognize anything other than a pair of numbers with dots in between.

bash script is giving me an error when it runs on MAC terminal

myfirst.sh is the file and the code I have is -
#! /bin/bash
number = 1
while [ $number -lt 10 ]
do
echo "$number"
number=$(( number+1 ))
done
when I run this I get the following error
./myfirst.sh: line 3: number: command not found
./myfirst.sh: line 5: [: -lt: unary operator expected
Remove spaces when assigning 1 to number
#! /bin/bash
number=1
while [ $number -lt 10 ]
do
echo "$number"
number=$(( number+1 ))
done
Output:
1
2
3
4
5
6
7
8
9
You cannot put spaces around = when you declare variables. It must be number=1

Shell scripting while loop error [duplicate]

This question already has answers here:
How do I compare two string variables in an 'if' statement in Bash? [duplicate]
(12 answers)
Why should there be spaces around '[' and ']' in Bash?
(5 answers)
Closed 6 years ago.
#!/bin/bash
a=0
while ["$a" -lt 10]
do
a=`expr "$a" + 1`
echo $a
done
This is the error:
a.sh: 3: a.sh: [0: not found
you need to keep spaces around your brackets:
while [ "$a" -lt 10 ]
since bash interpretes strings; it uses spaces to separate words. When you write ["$a"; then bash reads: [0 (after $a is replaced with 0)

if else dividing in BASH [duplicate]

This question already has answers here:
Arithmetic expressions in Bash?
(5 answers)
Closed 6 years ago.
I am doing a school assignment in bash and got this code:
if a < 0
a = a/b
else
a = b/a
fi
The assignment says that we need to divide two number read from the keyboard, and check if the first number is larger than the number 0.
echo "Write two numbers, with a space, that need to be divided:"
read a b
if a > 0
a = $a / $b
else
a = $b / $a
fi
echo "$a"
What am I doing wrong here?
Creating a math context in bash uses (( )). Note that bash only supports integer math natively -- be sure you aren't expecting fractional output (or using fractional inputs), and see BashFAQ #22 if this limitation is relevant to you.
if (( a > 0 )); then
a=$(( a / b ))
else
a=$(( b / a ))
fi

Is $(( expr )) equivalent to $[ expr ] in bash? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
bash: $[<arithmetic-expression>] vs. $((<arithmetic-expression>))
The $(( expr )) construct can be used for integer math in bash, e.g.
echo $(( 2*2 + 1 )) # 5
$[ expr ] seems to do to do the same (but isn't documented):
echo $[ 2*2 + 1 ] # 5
Are these constructs equivalent in bash?
man bash says:
The format for arithmetic expansion is:
$((expression))
The old format $[expression] is deprecated and will be removed in upcoming versions of bash.

Resources