Skip to main content
missing quotes
Source Link
Stéphane Chazelas
  • 586.2k
  • 96
  • 1.1k
  • 1.7k

Just use ksh (ksh93 precisely) or zsh, which both natively support floating point arithmetics:

$ cat test.ksh #!/bin/ksh min=12.45 val=10.35 if (( $val < $min )) ; then min=$val fi echo $min"$min" $ ./test.ksh 10.35 

Edit: Sorry, I missed ksh93 was already suggested. Keeping my answer just to make clear the script posted in the opening question can be used with no change outside the shell switch.

Edit2: Note that ksh93 requires the variable content to be consistent with your locale, i.e. with a French locale, a comma instead of a dot must be used:

... min=12,45 val=10,35 ... 

A more robust solution is to set the locale at the beginning of the script to make sure it will work regardless of the user's locale:

... export LC_ALL=C min=12.45 val=10.35 ... 

Just use ksh (ksh93 precisely) or zsh, which both natively support floating point arithmetics:

$ cat test.ksh #!/bin/ksh min=12.45 val=10.35 if (( $val < $min )) ; then min=$val fi echo $min $ ./test.ksh 10.35 

Edit: Sorry, I missed ksh93 was already suggested. Keeping my answer just to make clear the script posted in the opening question can be used with no change outside the shell switch.

Edit2: Note that ksh93 requires the variable content to be consistent with your locale, i.e. with a French locale, a comma instead of a dot must be used:

... min=12,45 val=10,35 ... 

A more robust solution is to set the locale at the beginning of the script to make sure it will work regardless of the user's locale:

... export LC_ALL=C min=12.45 val=10.35 ... 

Just use ksh (ksh93 precisely) or zsh, which both natively support floating point arithmetics:

$ cat test.ksh #!/bin/ksh min=12.45 val=10.35 if (( $val < $min )) ; then min=$val fi echo "$min" $ ./test.ksh 10.35 

Edit: Sorry, I missed ksh93 was already suggested. Keeping my answer just to make clear the script posted in the opening question can be used with no change outside the shell switch.

Edit2: Note that ksh93 requires the variable content to be consistent with your locale, i.e. with a French locale, a comma instead of a dot must be used:

... min=12,45 val=10,35 ... 

A more robust solution is to set the locale at the beginning of the script to make sure it will work regardless of the user's locale:

... export LC_ALL=C min=12.45 val=10.35 ... 
deleted 4 characters in body
Source Link
jlliagre
  • 62.5k
  • 11
  • 124
  • 162

Just use ksh (ksh93 precisely) or zsh, which both natively support floating point arithmetics:

$ cat test.ksh #!/bin/ksh min=12.45 val=10.35 if (( $val < $min )) ; then min=$val fi echo $min $ ./test.ksh 10.35 

Edit: Sorry, I missed ksh93 was already suggested. Keeping my answer just to make clear the script posted in the opening question can be used with no change outside the shell switch.

Edit2: Note that ksh93 requires the variable content to be consistent with your locale, i.e. with a French locale, a comma instead of a dot must be used:

... min=12,45 val=10,35 ... 

A more robust solution is to set the locale at the beginning of the script to make sure it will work regardless of the user's locale:

... export LC_NUMERIC=CLC_ALL=C min=12.45 val=10.35 ... 

Just use ksh (ksh93 precisely) or zsh, which both natively support floating point arithmetics:

$ cat test.ksh #!/bin/ksh min=12.45 val=10.35 if (( $val < $min )) ; then min=$val fi echo $min $ ./test.ksh 10.35 

Edit: Sorry, I missed ksh93 was already suggested. Keeping my answer just to make clear the script posted in the opening question can be used with no change outside the shell switch.

Edit2: Note that ksh93 requires the variable content to be consistent with your locale, i.e. with a French locale, a comma instead of a dot must be used:

... min=12,45 val=10,35 ... 

A more robust solution is to set the locale at the beginning of the script to make sure it will work regardless of the user's locale:

... export LC_NUMERIC=C min=12.45 val=10.35 ... 

Just use ksh (ksh93 precisely) or zsh, which both natively support floating point arithmetics:

$ cat test.ksh #!/bin/ksh min=12.45 val=10.35 if (( $val < $min )) ; then min=$val fi echo $min $ ./test.ksh 10.35 

Edit: Sorry, I missed ksh93 was already suggested. Keeping my answer just to make clear the script posted in the opening question can be used with no change outside the shell switch.

Edit2: Note that ksh93 requires the variable content to be consistent with your locale, i.e. with a French locale, a comma instead of a dot must be used:

... min=12,45 val=10,35 ... 

A more robust solution is to set the locale at the beginning of the script to make sure it will work regardless of the user's locale:

... export LC_ALL=C min=12.45 val=10.35 ... 
added 425 characters in body
Source Link
jlliagre
  • 62.5k
  • 11
  • 124
  • 162

Just use ksh (ksh93 precisely) or zsh, which both natively support floating point arithmetics:

$ cat test.ksh #!/bin/ksh min=12.45 val=10.35 if (( $val < $min )) ; then min=$val fi echo $min $ ./test.ksh 10.35 

Edit: Sorry, I missed ksh93ksh93 was already suggested. Keeping my answer just to make clear the script posted in the opening question can be used with no change outside the shell switch.

Edit2: Note that ksh93 requires the variable content to be consistent with your locale, i.e. with a French locale, a comma instead of a dot must be used:

... min=12,45 val=10,35 ... 

A more robust solution is to set the locale at the beginning of the script to make sure it will work regardless of the user's locale:

... export LC_NUMERIC=C min=12.45 val=10.35 ... 

Just use ksh (ksh93 precisely) or zsh, which both natively support floating point arithmetics:

$ cat test.ksh #!/bin/ksh min=12.45 val=10.35 if (( $val < $min )) ; then min=$val fi echo $min $ ./test.ksh 10.35 

Edit: Sorry, I missed ksh93 was already suggested. Keeping my answer just to make clear the script posted in the opening question can be used with no change outside the shell switch.

Just use ksh (ksh93 precisely) or zsh, which both natively support floating point arithmetics:

$ cat test.ksh #!/bin/ksh min=12.45 val=10.35 if (( $val < $min )) ; then min=$val fi echo $min $ ./test.ksh 10.35 

Edit: Sorry, I missed ksh93 was already suggested. Keeping my answer just to make clear the script posted in the opening question can be used with no change outside the shell switch.

Edit2: Note that ksh93 requires the variable content to be consistent with your locale, i.e. with a French locale, a comma instead of a dot must be used:

... min=12,45 val=10,35 ... 

A more robust solution is to set the locale at the beginning of the script to make sure it will work regardless of the user's locale:

... export LC_NUMERIC=C min=12.45 val=10.35 ... 
added 9 characters in body
Source Link
jlliagre
  • 62.5k
  • 11
  • 124
  • 162
Loading
added 8 characters in body
Source Link
Stéphane Chazelas
  • 586.2k
  • 96
  • 1.1k
  • 1.7k
Loading
Source Link
jlliagre
  • 62.5k
  • 11
  • 124
  • 162
Loading