0

I'm getting an error ((standard_in) 1: syntax error) trying to run the below code in a bash script. Could someone please point me in the correct direction?

if [[ $(bc <<< "$p0value > $freezeMax") ]]; then
        vP0='<a href="f1.php" class="blink">[Freezer 1: '
        vP0=$vP0$p0value
        vP0="$vP0 &deg;C]</a>"
        tempDIFF=$( bc <<< "$p0value-$freezeMax")
        echo "$P0_name is currently at $p0value °C, which is $tempDIFF °C higher than it should be. Please attend to this." >> $emailPATH/email.txt
        sendP0=1
elif [[ $(bc <<< "$p0value < $freezeMin") ]]; then
        vP0='<a href="f1.php" class="blink">[Freezer 1: '
        vP0=$vP0$p0value
        vP0="$vP0 &deg;C]</a>"
        tempDIFF=$(bc <<< "$freezeMin-$p0value")
        echo "$P0_name is currently at $p0value °C, which is $tempDIFF °C lower than it should be. Please attend to this." >> $emailPATH/email.txt
        sendP0=1
else
        vP0='<a href="f1.php" class="steady">[Freezer 1: '
        vP0=$vP0$p0value
        vP0="$vP0 &deg;C]</a>"
        sendP0=0
fi

FYI: the variables contain float numbers so I need to use bc to do the calculations.
Extra Info:
I'm running this on a Raspberry Pi 2 running Raspbian Jessie.
The shebang is #! /bin/bash

  • 2
    As a general rule for debugging this, do display the actual values that you send to bc before actually doing so. For example: printf 'freezeMin=%s\n' "$freezeMin". This way you will detect whether a variable is empty or has an unexpected value. The script, as it stands above, is also incomplete as it is missing the initialization of most variables. This makes it very difficult to test run for us. – Kusalananda Jan 13 '17 at 12:33
  • How do you run the script? – choroba Jan 13 '17 at 12:50
  • @Kusalananda Thanks, I'll echo out the values when i work on it again a bit later and let you know what they are. Unfortunately the script is over 1500 lines of code and putting it in here would be a problem. The variables such as $freezeMax and $freezeMin are stored in another settings file that's included in the 1st line of code source /usr/local/bin/temp/settings.conf and the permissions on that file are 777 for the time being. – Jim Jan 13 '17 at 13:08
  • @choroba I've got a cron job running it once per minute with chronic, and while testing I'm running it from the CLI using `./scriptname.sh' (not the actual script name) ;-) from the directory where it's located. That's how i'm getting the syntax error message. – Jim Jan 13 '17 at 13:11
1

bc will output 1 if condition is true

test should be

if [ $( bc <<< "test" ) == 1 ]

reading man page about [[ indicate this will test length of string, this might not be what you want.

  • or (( $( bc <<< '...' ) )) – choroba Jan 13 '17 at 13:10
  • Thanks, I'll test this as soon as possible. I'll be honest and admit that I've never been able to figure out when to use if [[ vs if [ vs if (( vs if ( vs plain if, so I just use [[ for everything. It's worked until now. – Jim Jan 13 '17 at 13:18
  • 2
    @Jim: Use [ ... ] for POSIX compatibility, but always with double quotes. Use [[ ... ]] if only bash is needed, you don't have to quote and you can use patterns, regular expressions, and && and || directly inside the brackets; use (( ... )) for arithmetic conditions. See man bash for details. – choroba Jan 13 '17 at 13:50
3

The error comes from bc, not the shell running your script. Perhaps one of the variables is empty?

 ~ $ bc <<< " < 1"
(standard_in) 1: syntax error
  • Close, I solved the (one) problem with using the correct bracketing ((( in place of [[). The other problem (I only figured out that I had another problem after I changed the bracketing) was that I was trying to subtract from a negative number, the notation would be: 5--18, which bc doesn't handle, so I put the second variable in it's own brackets making the notation 5-(-18) which it does handle. The reason I didn't notice this was that I have 7 instances of the above code and was getting 8 errors – Jim Jan 13 '17 at 15:22
  • 1
    @Jim: Note that a space would work, too. – choroba Jan 13 '17 at 15:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.