0

I'm trying to write a shell script and plan to calculate a simple division using two variables inside the script. I couldn't get it to work. It's some kind of syntax error.

Here is part of my code, named test.sh

    awk '{a+=$5} END {print a}' $variable1 > casenum
    awk '{a+=$5} END {print a}' $variable2 > controlnum
    score=$(echo "scale=4; $casenum/$controlnum" | bc)
    printf "%s\t%s\t%.4f\n", $variable3 $variable4 $score

It's just the $score that doesn't work.

I tried to use either

sh test.sh

or

bash test.sh

but neither worked. The error message is:

(standard_in) 1: syntax error

Does anyone know how to make it work? Thanks so much!

2
  • Do you intend for awk to read from a file named in the variable $variable1? And do you really want the output of that awk script to go to a file named casenum which happens to be the name of a variable that you use in the echo? I suspect your problems are primarily the result of confusion about basic shell syntax. Commented Oct 29, 2013 at 14:31
  • exactly, fedorqui's answer mentioned the same thing. thanks! Commented Oct 29, 2013 at 15:35

3 Answers 3

2

You are outputting to files, not to vars. For this, you need var=$(command). Hence, this should make it:

casenum=$(awk '{a+=$5} END {print a}' $variable1)
controlnum=$(awk '{a+=$5} END {print a}' $variable2)
score=$(echo "scale=4; $casenum/$controlnum" | bc)
printf "%s\t%s\t%.4f\n", $variable3 $variable4 $score

Note $variable1 and $variable2 should be file names. Otherwise, indicate it.

Sign up to request clarification or add additional context in comments.

3 Comments

$variable1 and $variable2 are variables in my script. They were working fine. What would you do to indicate it?
i see. For simplicity I didn't include the previous lines. They are used as variable. I just accepted it!
Great, now I understand, @user2157668 :)
1

First your $variable1 and $variable2 must expand to a name of an existing file; but that's not a syntax error, it's just a fact that makes your code wrong, unless you mean really to cope with files containing numbers and accumulating the sum of the fifth field into a file. Since casenum and controlnum are not assigned (in fact you write the awk result to a file, not into a variable), your score computation expands to

score=$(echo "scale=4; /" | bc)

which is wrong (Syntax error comes from this).

Then, the same problem with $variable3 and $variable4. Are they holding a value? Have you assigned them with something like

  variable=...

? Otherwise they will expand as "". Fixing these (including assigning casenum and controlnum), will fix everything, since basically the only syntax error is when bc tries to interpret the command / without operands. (And the comma after the printf is not needed).

The way you assign the output of execution of a command to a variable is

  var=$(command)

or

  var=`command`

Comments

1

If I understand your commands properly, you could combine calculation of score with a single awk statement as follows

score=$(awk 'NR==FNR {a+=$5; next} {b+=$5} END {printf "%.4f", a/b}' $variable1 $variable2)

This is with assumption that $variable1 and $variable2 are valid file names

Refer to @fedorqui's solution if you want to stick to your approach of 2 awk and 1 bc.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.