Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

170
Views
Bash comparando números flotantes usando el cumplimiento de POSIX

mi caso es muy sencillo:

Estoy tratando de comparar dos valores:

 a=0.2 b=0.1

El código que estoy tratando de ejecutar es:

 if [ "$a" -gt "$b" ]; then echo "You are running an outdated version" fi
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Suponiendo que desea comparar los números de versión, intente lo siguiente:

 #!/bin/bash -posix # compares version numbers # prints 0 if $a == $b # positive number if $a is newer than $b # negative number if $a is older than $b vercmp() { local a=$1 local b=$2 local a1=${a%%.*} # major number of $a local b1=${b%%.*} # major number of $b if [[ $a = "" ]]; then if [[ $b = "" ]]; then echo 0 # both $a and $b are empty else vercmp "0" "$b" fi elif [[ $b = "" ]]; then vercmp "$a" "0" elif (( 10#$a1 == 10#$b1 )); then local a2=${a#*.} # numbers after the 1st dot if [[ $a2 = $a ]]; then a2="" # no more version numbers fi local b2=${b#*.} # numbers after the 1st dot if [[ $b2 = $b ]]; then b2="" # no more version numbers fi vercmp "$a2" "$b2" else echo $(( 10#$a1 - 10#$b1 )) fi }

Ejemplos:

 vercmp 0.2 0.1 => 1 (positive number: the former is newer) vercmp 1.0.2 1.0.10 => -8 (negative number: the latter is newer) a=0.2 b=0.1 if (( $(vercmp "$a" "$b") > 0 )); then echo "You are running an outdated version" fi
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!