-1

I wrote this code to get temperature in floating point in bash. this gives me an error:

line 16: [: missing ']'

Here is the code:

#!/bin/bash
echo "Celsius         Fahrenheit"
echo "--------------------------"
counter=0
while [ $counter -le 25 ]:
do
  let "val = ($counter * 9/5) + 32"
  if [ $counter -le 9 ]
  then
    whitespace="               "
  else
    whitespace="              "
  fi
  echo "$counter$whitespace$val"
  ((counter++))
if [[ "$REPLY" =~ ^-?[[:digit:]]*\.[[:digit:]]+$ ]]; then
   echo "'$REPLY' is a floating point number."
fi

done
exit 1
  • 1
    Possible duplicate of [Escaping square bracket in bash inside a string for an if](https://stackoverflow.com/q/11419668/608639), [How to compare strings containing a square bracket in Bash](https://stackoverflow.com/q/46231965/608639), etc. – jww Nov 26 '19 at 07:49

2 Answers2

0

Your while is using colon (:) when it should be a semicolon (;):

#!/bin/bash
echo "Celsius         Fahrenheit"
echo "--------------------------"
counter=0
while [ $counter -le 25 ];
do
  let "val = ($counter * 9/5) + 32"
  if [ $counter -le 9 ]
  then
    whitespace="               "
  else
    whitespace="              "
  fi
  echo "$counter$whitespace$val"
  ((counter++))
if [[ "$REPLY" =~ ^-?[[:digit:]]*\.[[:digit:]]+$ ]]; then
   echo "'$REPLY' is a floating point number."
fi

done
exit 1

Output:

$ /usr/bin/diff old.sh new.sh 
5c5
< while [ $counter -le 25 ]:
---
> while [ $counter -le 25 ];

$ sh old.sh 
Celsius         Fahrenheit
--------------------------
old.sh: line 5: [: missing `]'

$ sh new.sh 
Celsius         Fahrenheit
--------------------------
0               32
1               33
2               35
3               37
4               39
5               41
6               42
7               44
8               46
9               48
10              50
11              51
12              53
13              55
14              57
15              59
16              60
17              62
18              64
19              66
20              68
21              69
22              71
23              73
24              75
25              77
richyen
  • 8,114
  • 4
  • 13
  • 28
  • Line 16 is the *`if [[ "$REPLY" ...`*. – jww Nov 26 '19 at 07:50
  • When I copy/paste OP's code, my error appears at line 5, not 16--so either OP needs to provide correct example or correct error – richyen Nov 26 '19 at 07:52
  • thank you, but there is still a problem, I am not getting result in decimal point? for example 77.0 or 60.0 – saleem stationwala Nov 26 '19 at 08:05
  • Part of the problem is you never define `REPLY`—could you update your example with what you have tried? – richyen Nov 26 '19 at 08:32
  • Also, you should be using `bc` somewhere : https://stackoverflow.com/questions/12722095/how-do-i-use-floating-point-division-in-bash – richyen Nov 26 '19 at 08:39
0

use bc will give floating point results

val=bc <<<"scale=2;($counter*9/5)+32"

#!/bin/bash
echo "Celsius         Fahrenheit"
echo "--------------------------"
counter=0
while [ $counter -le 25 ];
do
 val=`bc  <<<"scale=2;($counter*9/5)+32"`

  if [ $counter -le 9 ]
  then
    whitespace="               "
  else
    whitespace="              "
  fi
  echo "$counter$whitespace$val"
  ((counter++))
if [[ "$REPLY" =~ ^-?[[:digit:]]*\.[[:digit:]]+$ ]]; then
   echo "'$REPLY' is a floating point number."
fi

done
exit 1

The answer is

Celsius         Fahrenheit
--------------------------
0               32.00
1               33.80
2               35.60
3               37.40
4               39.20
5               41.00
6               42.80
7               44.60
8               46.40
9               48.20
10              50.00
11              51.80
12              53.60
13              55.40
14              57.20
15              59.00
16              60.80
17              62.60
18              64.40
19              66.20
20              68.00
21              69.80
22              71.60
23              73.40
24              75.20
25              77.00

If it helps hit correct answer

vkkindia
  • 55
  • 7