how to convert a string to a float and then use it in a conditional operation? [duplicate]
Clash Royale CLAN TAG#URR8PPP
This question already has an answer here:
How to compare a program's version in a shell script?
3 answers
I'm trying to write my own bash script that detects if Debian's release version is less than 9.0
code:
VERSION=$(cat /etc/debian_version)
echo $VERSION
if [ "$VERSION" -lt "9.0" ]; then
echo "Debian version is less than 9"
else
echo "Debian version is greater than 9"
fi
but the error i'm receiving is:
./test.sh: line 4: [: 8.2: integer expression expected
basically, $VERSION
is a string type, but it's 8.2
. I'm trying to make it become 8.2
, but as a float. so I can use <=
on it.
bash shell-script
marked as duplicate by Jeff Schaller, A.B, Stephen Harris, Thomas, icarus Dec 25 '18 at 17:21
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
How to compare a program's version in a shell script?
3 answers
I'm trying to write my own bash script that detects if Debian's release version is less than 9.0
code:
VERSION=$(cat /etc/debian_version)
echo $VERSION
if [ "$VERSION" -lt "9.0" ]; then
echo "Debian version is less than 9"
else
echo "Debian version is greater than 9"
fi
but the error i'm receiving is:
./test.sh: line 4: [: 8.2: integer expression expected
basically, $VERSION
is a string type, but it's 8.2
. I'm trying to make it become 8.2
, but as a float. so I can use <=
on it.
bash shell-script
marked as duplicate by Jeff Schaller, A.B, Stephen Harris, Thomas, icarus Dec 25 '18 at 17:21
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
By the way, Bash doesn't support float numbers, it only works with integers.
– Giovanni Nunes
Dec 24 '18 at 16:03
ty @GiovanniNunes is it possible that bash is going to add support for float in the future?
– girng rodriguez
Dec 24 '18 at 20:25
add a comment |
This question already has an answer here:
How to compare a program's version in a shell script?
3 answers
I'm trying to write my own bash script that detects if Debian's release version is less than 9.0
code:
VERSION=$(cat /etc/debian_version)
echo $VERSION
if [ "$VERSION" -lt "9.0" ]; then
echo "Debian version is less than 9"
else
echo "Debian version is greater than 9"
fi
but the error i'm receiving is:
./test.sh: line 4: [: 8.2: integer expression expected
basically, $VERSION
is a string type, but it's 8.2
. I'm trying to make it become 8.2
, but as a float. so I can use <=
on it.
bash shell-script
This question already has an answer here:
How to compare a program's version in a shell script?
3 answers
I'm trying to write my own bash script that detects if Debian's release version is less than 9.0
code:
VERSION=$(cat /etc/debian_version)
echo $VERSION
if [ "$VERSION" -lt "9.0" ]; then
echo "Debian version is less than 9"
else
echo "Debian version is greater than 9"
fi
but the error i'm receiving is:
./test.sh: line 4: [: 8.2: integer expression expected
basically, $VERSION
is a string type, but it's 8.2
. I'm trying to make it become 8.2
, but as a float. so I can use <=
on it.
This question already has an answer here:
How to compare a program's version in a shell script?
3 answers
bash shell-script
bash shell-script
edited Dec 24 '18 at 7:05
msp9011
3,80843863
3,80843863
asked Dec 24 '18 at 5:55
girng rodriguez
33
33
marked as duplicate by Jeff Schaller, A.B, Stephen Harris, Thomas, icarus Dec 25 '18 at 17:21
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by Jeff Schaller, A.B, Stephen Harris, Thomas, icarus Dec 25 '18 at 17:21
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
By the way, Bash doesn't support float numbers, it only works with integers.
– Giovanni Nunes
Dec 24 '18 at 16:03
ty @GiovanniNunes is it possible that bash is going to add support for float in the future?
– girng rodriguez
Dec 24 '18 at 20:25
add a comment |
By the way, Bash doesn't support float numbers, it only works with integers.
– Giovanni Nunes
Dec 24 '18 at 16:03
ty @GiovanniNunes is it possible that bash is going to add support for float in the future?
– girng rodriguez
Dec 24 '18 at 20:25
By the way, Bash doesn't support float numbers, it only works with integers.
– Giovanni Nunes
Dec 24 '18 at 16:03
By the way, Bash doesn't support float numbers, it only works with integers.
– Giovanni Nunes
Dec 24 '18 at 16:03
ty @GiovanniNunes is it possible that bash is going to add support for float in the future?
– girng rodriguez
Dec 24 '18 at 20:25
ty @GiovanniNunes is it possible that bash is going to add support for float in the future?
– girng rodriguez
Dec 24 '18 at 20:25
add a comment |
2 Answers
2
active
oldest
votes
Try this, I use to compare the float with bc
.
VERSION=$(cat /etc/debian_version)
echo $VERSION
if [ "`echo "$VERSION < 9.0" | bc`" -eq 1 ]; then
echo "Debian version is less than 9"
else
echo "Debian version is greater than 9"
fi
one-liner using awk
awk 'if ($1 < 9.0) print "Debian version is less than 9"; else if ($1 > 9.0) print "Debian version is greater than 9";' /etc/debian_version
hi msp9011, i'm getting./test.sh: line 5: bc: command not found ./test.sh: line 5: [: : integer expression expected
– girng rodriguez
Dec 24 '18 at 7:14
1
oh. i just didapt-get install bc
. i think it's working now. thank you so much.
– girng rodriguez
Dec 24 '18 at 7:16
add a comment |
Version there isn't a simple float number and version 1.10 is greater than 1.9. I think that a solution using sort
utility, that knows how to handle versions numbers:
check_versions()
# check_version <reference> <version to check>
if [[ $1 == $2 ]]; then
echo 'same'
else
low=$(echo -e "$1n$2"
ref='8.5' # reference version for check
check_versions $ref '8.2' # current version is lower!
check_versions $ref '8.5' # current version is equal!
check_versions $ref '8.12' # current version is greater!
I think that it should a better solution.
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Try this, I use to compare the float with bc
.
VERSION=$(cat /etc/debian_version)
echo $VERSION
if [ "`echo "$VERSION < 9.0" | bc`" -eq 1 ]; then
echo "Debian version is less than 9"
else
echo "Debian version is greater than 9"
fi
one-liner using awk
awk 'if ($1 < 9.0) print "Debian version is less than 9"; else if ($1 > 9.0) print "Debian version is greater than 9";' /etc/debian_version
hi msp9011, i'm getting./test.sh: line 5: bc: command not found ./test.sh: line 5: [: : integer expression expected
– girng rodriguez
Dec 24 '18 at 7:14
1
oh. i just didapt-get install bc
. i think it's working now. thank you so much.
– girng rodriguez
Dec 24 '18 at 7:16
add a comment |
Try this, I use to compare the float with bc
.
VERSION=$(cat /etc/debian_version)
echo $VERSION
if [ "`echo "$VERSION < 9.0" | bc`" -eq 1 ]; then
echo "Debian version is less than 9"
else
echo "Debian version is greater than 9"
fi
one-liner using awk
awk 'if ($1 < 9.0) print "Debian version is less than 9"; else if ($1 > 9.0) print "Debian version is greater than 9";' /etc/debian_version
hi msp9011, i'm getting./test.sh: line 5: bc: command not found ./test.sh: line 5: [: : integer expression expected
– girng rodriguez
Dec 24 '18 at 7:14
1
oh. i just didapt-get install bc
. i think it's working now. thank you so much.
– girng rodriguez
Dec 24 '18 at 7:16
add a comment |
Try this, I use to compare the float with bc
.
VERSION=$(cat /etc/debian_version)
echo $VERSION
if [ "`echo "$VERSION < 9.0" | bc`" -eq 1 ]; then
echo "Debian version is less than 9"
else
echo "Debian version is greater than 9"
fi
one-liner using awk
awk 'if ($1 < 9.0) print "Debian version is less than 9"; else if ($1 > 9.0) print "Debian version is greater than 9";' /etc/debian_version
Try this, I use to compare the float with bc
.
VERSION=$(cat /etc/debian_version)
echo $VERSION
if [ "`echo "$VERSION < 9.0" | bc`" -eq 1 ]; then
echo "Debian version is less than 9"
else
echo "Debian version is greater than 9"
fi
one-liner using awk
awk 'if ($1 < 9.0) print "Debian version is less than 9"; else if ($1 > 9.0) print "Debian version is greater than 9";' /etc/debian_version
edited Dec 24 '18 at 7:03
answered Dec 24 '18 at 6:30
msp9011
3,80843863
3,80843863
hi msp9011, i'm getting./test.sh: line 5: bc: command not found ./test.sh: line 5: [: : integer expression expected
– girng rodriguez
Dec 24 '18 at 7:14
1
oh. i just didapt-get install bc
. i think it's working now. thank you so much.
– girng rodriguez
Dec 24 '18 at 7:16
add a comment |
hi msp9011, i'm getting./test.sh: line 5: bc: command not found ./test.sh: line 5: [: : integer expression expected
– girng rodriguez
Dec 24 '18 at 7:14
1
oh. i just didapt-get install bc
. i think it's working now. thank you so much.
– girng rodriguez
Dec 24 '18 at 7:16
hi msp9011, i'm getting
./test.sh: line 5: bc: command not found ./test.sh: line 5: [: : integer expression expected
– girng rodriguez
Dec 24 '18 at 7:14
hi msp9011, i'm getting
./test.sh: line 5: bc: command not found ./test.sh: line 5: [: : integer expression expected
– girng rodriguez
Dec 24 '18 at 7:14
1
1
oh. i just did
apt-get install bc
. i think it's working now. thank you so much.– girng rodriguez
Dec 24 '18 at 7:16
oh. i just did
apt-get install bc
. i think it's working now. thank you so much.– girng rodriguez
Dec 24 '18 at 7:16
add a comment |
Version there isn't a simple float number and version 1.10 is greater than 1.9. I think that a solution using sort
utility, that knows how to handle versions numbers:
check_versions()
# check_version <reference> <version to check>
if [[ $1 == $2 ]]; then
echo 'same'
else
low=$(echo -e "$1n$2"
ref='8.5' # reference version for check
check_versions $ref '8.2' # current version is lower!
check_versions $ref '8.5' # current version is equal!
check_versions $ref '8.12' # current version is greater!
I think that it should a better solution.
add a comment |
Version there isn't a simple float number and version 1.10 is greater than 1.9. I think that a solution using sort
utility, that knows how to handle versions numbers:
check_versions()
# check_version <reference> <version to check>
if [[ $1 == $2 ]]; then
echo 'same'
else
low=$(echo -e "$1n$2"
ref='8.5' # reference version for check
check_versions $ref '8.2' # current version is lower!
check_versions $ref '8.5' # current version is equal!
check_versions $ref '8.12' # current version is greater!
I think that it should a better solution.
add a comment |
Version there isn't a simple float number and version 1.10 is greater than 1.9. I think that a solution using sort
utility, that knows how to handle versions numbers:
check_versions()
# check_version <reference> <version to check>
if [[ $1 == $2 ]]; then
echo 'same'
else
low=$(echo -e "$1n$2"
ref='8.5' # reference version for check
check_versions $ref '8.2' # current version is lower!
check_versions $ref '8.5' # current version is equal!
check_versions $ref '8.12' # current version is greater!
I think that it should a better solution.
Version there isn't a simple float number and version 1.10 is greater than 1.9. I think that a solution using sort
utility, that knows how to handle versions numbers:
check_versions()
# check_version <reference> <version to check>
if [[ $1 == $2 ]]; then
echo 'same'
else
low=$(echo -e "$1n$2"
ref='8.5' # reference version for check
check_versions $ref '8.2' # current version is lower!
check_versions $ref '8.5' # current version is equal!
check_versions $ref '8.12' # current version is greater!
I think that it should a better solution.
answered Dec 24 '18 at 23:44
Giovanni Nunes
1112
1112
add a comment |
add a comment |
By the way, Bash doesn't support float numbers, it only works with integers.
– Giovanni Nunes
Dec 24 '18 at 16:03
ty @GiovanniNunes is it possible that bash is going to add support for float in the future?
– girng rodriguez
Dec 24 '18 at 20:25