0

I have a problem whenever I add more than 3 numbers with multiple operators. (I tried expr, bc,

SUM=$(( $S1 + $S2 + $S3 ))

and many other forms, but whenever I have 3 variables I get this error.

expr: non-integer argument
expr: syntax error

This is when I do it with 2 variables (works fine)

#!/bin/sh
FILE=$1
while read -r SID FIRST LAST S1 S2 S3
do
     SUM=$(expr $S1 + $S2)
     AVG=$(expr $SUM / 3)
     printf '%d [%d] %s, %s\n' "$AVG" "$SID" "$LAST" "$FIRST"
done < "$FILE" | sort

and when I do 3 variables (doesn't work)

#!/bin/sh
FILE=$1
while read -r SID FIRST LAST S1 S2 S3
do
     SUM=$(expr $S1 + $S2 + $S3)
     AVG=$(expr $SUM / 3)
     printf '%d [%d] %s, %s\n' "$AVG" "$SID" "$LAST" "$FIRST"
done < "$FILE" | sort


expr: non-integer argument
expr: syntax error

txt file

123456789 Lee Johnson 72 85 90
999999999 Jaime Smith 90 92 91
888111818 JC Forney 100 81 97
290010111 Terry Lee 100 99 100
199144454 Tracey Camp 77 84 84
299226663 Laney Camp 70 74 71
434401929 Skyler Camp 78 81 82
928441032 Jess Forester 85 80 82
928441032 Chris Forester 97 94 89
ellsusan
  • 91
  • 1
  • 1
  • 6
  • have you tried S2 + S3 ? – John3136 Sep 09 '16 at 02:55
  • Nope. You absolutely can add three integers; this just means that not all your variables are integers. – Charles Duffy Sep 09 '16 at 02:59
  • In the future, by the way, make a point of including all data necessary to reproduce an error in the question itself, by the way, rather than requiring anyone who wants to test their answer to build a data file matching your format.. – Charles Duffy Sep 09 '16 at 03:01
  • btw, using all-caps names for your own variables is bad form -- all-caps names are used for variables with meaning to the shell or operating system. See naming conventions given in the fourth paragraph of the POSIX specification at http://pubs.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap08.html (on environment variables, but shell variables share a namespace, since defining a shell variable overwrites any like-named environment variable). – Charles Duffy Sep 09 '16 at 03:03
  • @CharlesDuffy Hey, thanks for trying to help. I'll use all-caps next time, and sorry! I forgot to add the txt file. I just added it. When I try to use S2 + S 3 it works, however, when I try to add all 3 variables I get the same error. Do you happen to see any problems with my txt file? (I'm using linux) – ellsusan Sep 09 '16 at 03:47
  • 1
    similar question been already asked (https://stackoverflow.com/questions/39401159/sorting-3-columns-and-getting-the-average) and problem definitely is dos style line ending.. dunno why OP just wouldn't try to address that problem – Sundeep Sep 09 '16 at 04:22

1 Answers1

2

The shell absolutely supports this; thus, the problem is with your data. Try the following:

s1=1
s2=2
s3=3
echo $(( s1 + s2 + s3 ))

...run, and showing output 6, here.


Likewise:

s1=1
s2=2
s3=3
expr "$s1" + "$s2" + "$s3"

...run, and showing output 6, here.

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
  • 2
    I'm guessing wildly here, but the data file might have Windows/DOS-style line endings (carriage return + linefeed), in which case the carriage return will be included in S3, making it not an integer. Another possibility is that the file has more than 6 fields, in which case all the extra ones will be included in S3, again making it non-integer. We'd need the data file to tell for sure. – Gordon Davisson Sep 09 '16 at 03:38