View Full Version : [SOLVED] bash scripting / (standard_in) 1: syntax error

April 12th, 2017, 12:01 AM
I'm learning bash scripting.

The following command...

Uavg=echo "scale=6; $Qavg/$Oarea" | bc`

does not assign a value to Uavg and gives this error...

(standard_in) 1: syntax error

previously, the variables $Qavg and $Oarea have been assigned with the expressions...

Qavg=`cat Uavg.dat | grep "areaIntegrate(outlet)" | head -1 | xargs | cut -d' ' -f7 | tr ')' ' '`

Oarea=`cat Uavg.dat | grep "total area" | cut -d'=' -f2`

I have checked that $Qavg and $Oarea have numerical values, but maybe previous command lines have an eol problem and the resulting variables are strings.

Any idea about this problem?

Thanks a lot

April 12th, 2017, 12:45 AM

Your code is overcomplicated hence is error-prone. You're a beginner, so there is nothing wrong with that.

It would be much more constructive if you could post the task what you want achieve (some example input files and desired output), so we can direct you to the sources where you can find your answers.

Note: We are happy to help with hints and suggestion with your homework, but don't expect someone else to solve it for you.

April 12th, 2017, 01:04 AM
You missed the ` at the start.

Uavg=`echo "scale=6; $Qavg/$Oarea" | bc`

April 12th, 2017, 10:11 AM
Thank you very much for the answers!

In the script I have Uavg=`echo "scale=6; $Qavg/$Oarea" | bc`. The ' missing was an error transcribing the command to the forum thread :(

What I'm trying to do is to read a couple of values from file Uavg.dat and assign them to the variables Qavg and Oarea, and then divide them to get the value of Uavg.

I wonder if the following commands may produce a string of characters instead of a number (by typing $Qavg and $Oarea they show as numbers):
Qavg=`cat Uavg.dat | grep "areaIntegrate(outlet)" | head -1 | xargs | cut -d' ' -f7 | tr ')' ' '`

Oarea=`cat Uavg.dat | grep "total area" | cut -d'=' -f2`

Maybe the first command is doing something wrong at the end, when is substituing ')' by ' ', and it results in a string rather than a number.

April 12th, 2017, 11:14 AM
I've found a way to solve the problem with a perl command, it seems that bash arithmetics can deal only with integer numbers.

The follwing command works:

Uavg=$(perl -e "print $Qavg/$Oarea")

Thank's again

April 12th, 2017, 11:19 AM
Thread moved to Programming Talk.