(standard_in)1:syntax error using bc with Cron

I created a shell script to record server temperature. When I manually run script it works fine with no error message. But when I create a cron job, my script fails with error message (standard_in)1:syntax error. I figured out the bc utility is causing the error message. Below is my script.

#!/bin/bash 


export SSHPASS=xxxxxxxxxxxx

# Connecting to DRESX iLO host via SSH
sshpass -e ssh -oBatchMode=no Administrator@192.168.15.71 show system1/sensor3 >> /usr/local/bin/Production/Templinegraph-script/DRESX5501/ilo5501-step1.csv

# Removing lines not needed from ilo5501-step1.csv file
sed '1,12 d;14,22 d' /usr/local/bin/Production/Templinegraph-script/DRESX5501/ilo5501-step1.csv > /usr/local/bin/Production/Templinegraph-script/DRESX5501/ilo5501-step2.csv

# Grabbing data I want after = sign from ilo5501-step2.csv and creating new file new CSV named step3.csv
sed 's/.*[=] *//' /usr/local/bin/Production/Templinegraph-script/DRESX5501/ilo5501-step2.csv > /usr/local/bin/Production/Templinegraph-script/DRESX5501/ilo5501-step3.csv

# Turning multiple lines into one line with comma separated
paste -d, -s /usr/local/bin/Production/Templinegraph-script/DRESX5501/ilo5501-step3.csv > /usr/local/bin/Production/Templinegraph-script/DRESX5501/ilo5501-step4.csv

# Removing ^M control M character from ilo5501-step4.csv
tr -d '\r' < /usr/local/bin/Production/Templinegraph-script/DRESX5501/ilo5501-step4.csv > /usr/local/bin/Production/Templinegraph-script/DRESX5501/ilo5501-step5.csv

# Celsius variable, calling step5.csv and making file into variable
celsius=$(</usr/local/bin/Production/Templinegraph-script/DRESX5501/ilo5501-step5.csv)

# Performing Celsius to Fahrenheit conversion
fahrenheit=$(bc <<< "1.8*$celsius+32")

# Sending Fahrenheit conversion to step6.csv
echo "$fahrenheit" > /usr/local/bin/Production/Templinegraph-script/senddb/ilo-step6.csv

Inside of fahrenheit variable I tried different variations of the bc command and nothing seems to work.

I have tried:

fahrenheit=$(echo '(1.8*$celsius+32)' | tr -d '\n' | bc -l)
fahrenheit=$(echo "1.8*$celsius+32" | bc -l)
fahrenheit=$(echo "1.8*$celsius;+32" | bc)
fahrenheit=$(bc <<<  "scale=2; 1.8*$celsius+32")

and nothing seems to work.

I've declared celsius as a variable by using ilo5501-step5.csv file. There is a numerical value with a decimal inside of ilo5501-step5.csv file. I'm using that value converting it to fahrenheit by calling celsuis inside of fahrenheit variable.
What is wrong with bc command? Em I using the bc command incorrectly when using cron to run script.

Sure it's bc ? Pls show your cron entry, and also the contents of the celsius variable, and the step5 file.

Are your aware that most of your convoluted processing could be done in one single script, e.g. awk or perl ?
Are there lines beyond 22 in the original file?

1 Like

I would expect echo '(1.8*$celsius+32)' | tr -d '\n' | bc -l to give you the diagnostic message you showed us since bc input needs to be a text file and, by stripping off the <newline> character with the tr element in your pipeline, bc is not getting a text file as input.

You should not get that error with echo "1.8*$celsius+32" | bc -l or with echo "1.8*$celsius+32" | bc , but you might get a different error if the celsius variable has not been set to a numeric string value.

Assuming you're trying to get a floating point result, if you have a 1993 or later version of ksh available on your system (in addition to bash ), you could use the much more efficient:

fahrenheit=$((1.8*$celsius+32))
1 Like

Thanks for your reply @RudiC.

The reason why I thought is was bc utility, when I received "(standard_in)1:sytax error" I noticed fahrenheit variable inside of ilo-step6.csv file isn't converted to Fahrenheit value. But after more testing my assumption was incorrect.

I'm almost convinced its not bc causing error code. I notice when I receive "(standard_in)1:sytax error" I'm not getting a temperature reading via SSH server. I know for a fact server isn't down. Which leads me to believe that SSH is failing to connect to server. I'm still diagnosing issue. Any new finding I will post my results.

Below is my cron entry:

  MAILTO="noc@sysadmin.com"


# First run
00,30 * * * * /usr/local/bin/Production/Templinegraph-script/DRESX5501/5501.sh

# Remove all CSV's files
05,35 * * * * /usr/local/bin/Production/Templinegraph-script/DRESX5501/rmCSV.sh

Below is contents of celsuis variable in step5 file:

21

I wasn't aware all my bash shell script could be done in one single script using awk and perl. That's interesting. I definitely need to learn more awk commands. I'll look into perl programming soon. I'm always trying to improve in bash shell. I'm still learning and having fun. Thank you.

Pls run the cron job with the -x (xtrace) bash option and post the output.

And, how far would

sshpass -e ssh -oBatchMode=no Administrator@192.168.15.71 show system1/sensor3 |
awk -F= 'NR == 13 {sub ("\r$", "", $NF); print 1.8 * $NF + 32); exit}' > ilo-step6.csv

get you? Note: this is untested; for a test pls post the sshpass command's full output.

2 Likes

I ran xtrace and result is below:

  + export 'SSHPASS=xxxxxxxxxxxxxxx'
  + SSHPASS='xxxxxxxxxxxx'
  + sshpass -e ssh -oBatchMode=no Administrator@192.168.15.71 show 
  + system1/sensor3 sed '1,12 d;14,22 d' 
  + /usr/local/bin/Production/Templinegraph-script/DRESX5501/ilo5501-step1
  + .csv sed 's/.*[=] *//' 
  + /usr/local/bin/Production/Templinegraph-script/DRESX5501/ilo5501-step2
  + .csv paste -d, -s 
  + /usr/local/bin/Production/Templinegraph-script/DRESX5501/ilo5501-step3
  + .csv
  + tr -d '\r'
  + celsius=
  ++ echo '1.8*+32'
  ++ bc
  (standard_in) 1: syntax error
  + fahrenheit=
  + echo ''

Looks like SSH is not connecting to the server and extracting temperature value. Going to look into why SSH isn't connecting the server. Also going to look into using a different SSH command.

I ran awk command and I received this output below:

  + sshpass -e ssh -oBatchMode=no Administrator@192.168.15.71 show system1/sensor3
+ awk -F= 'NR == 13 {sub ("\r$", "", $NF); print 1.8 * $NF + 32); exit}'
awk: cmd. line:1: NR == 13 {sub ("\r$", "", $NF); print 1.8 * $NF + 32); exit}
awk: cmd. line:1:                                                     ^ syntax error

I'm getting syntax error at

print 1.8 * $NF + 32);

part.

Sorry for the ) too many; as said I didn't have a chance to test as I was on a - huaaa - windows machine. Remove and try again.
Would it be possible the file names have \r in them as well? Your xtrace seems to have some indication.

Why what would happen if I change file name to \r?

You awk command worked great.

Wow! Going to read up on awk command. Thank you for showing me how to become a better bash programmer.

Thanks everyone for all the follow up replies.