2

So I when I run this code on my mac there are no errors, and it provides me the perfect output. But when I run it on Ubuntu or CentOS i get the following error integer expression expected

 #!/bin/bash
if [ -f $1 ] ;
then
    sum=0
    echo "#Name Surname City Amount"
    while read -r LINE || [[ -n $LINE ]]; do
        firstName=$( echo $LINE | cut -d " " -f1)
        lastName=$( echo $LINE | cut -d " " -f2)
        city=$( echo $LINE | cut -d " " -f3)
        amount=$( echo $LINE | cut -d " " -f9)
        check=$( echo $amount | grep -c "[0-9]")
        if [ $check -gt 0 ]; then
            if [ $amount -gt 999 ] ; then
                state=$(echo $LINE | cut -d " " -f5)
                correctState=$(echo $state | grep -c "^N[YCEJ]")
                if [ $correctState -gt 0 ] ; then
                    echo "$firstName $lastName $state $city $amount"
                    sum=`expr $sum + $amount`
                fi

            fi
        fi
    done < $1
    echo ""
    echo "The sum is all printed amounts is $sum"
    echo ""
else
    echo "No file found"
fi

Input File:

#Name Surname City Company State Probability Units Price Amount
Tony Passaquale Edenvale "Sams_Groceries_Inc. NJ 90 800 4.78 3824
Nigel Shanford Atlanta Fulton_Hotels_Inc. GA 40 400 9.99 3996
Selma Cooper Eugene Cooper_Inns OR 40 1000 9.99 9990
Allen James San_Jose City_Center_Lodge CA 40 1000 9.99 9990
Bruce Calaway Irvine Penny_Tree_Foods CA 80 1000 4.99 4990
Gloria Lenares Chicago Cordoba_Coffee_Shops IL 60 200 9.99 1998
Wendy Leach New_York Gourmet_Imports NY 100 100 10 1000
Craig Flanders Omaha Fly_n_Buy NE 40 1200 9.49 11388
Montgomery Weissenborn Chicago Shariott_Suites_Hotels IL 60 400 7.98 3192
Shirley Brightwell San_Francisco Pacific_Cafe_Company CA 80 2900 1.75 5075
Roger Vittorio Cleveland National_Associa OH 40 1000 9.99 9990
Tony Passaquale Edenvale Sams_Groceries NJ 90 1000 2.29 2290
Montgomery Weissenborn Los_Angeles Shariott_Suites_Hotels CA 90 5000 1.49 7450
Michael Wiggum Los_Angeles Trader_Depot CA 70 800 2.5 2000
Edna Brock Raleigh Elliott's_Department_Stores NC 70 14400 1.78 25632
Gloria Lenares Chicago Cordoba_Coffee_Shops IL 90 600 8.99 5394
Montgomery Weissenborn Seattle Shariott_Suites_Hotels WA 90 400 8.99 3596
Beth Munin Seattle Little_Corner_Sweets WA 100 400 1.39 556
Tim Kelly New_York Nuts_and_Things NY 60 100 9.99 999
Bart Perryman San_Francisco Kwik-e-mart CA 90 40000 0.69 27600
Stacey Gordon Irvine Penny_Tree_Foods CA 70 200 12.96 2592
Heather Willis Atlanta Big_Chuck_Diners GA 80 400 4.99 1996
Tim Kelly New_York Nuts_and_Things NY 70 600 1.49 894
Ralph Khan New_York Gigamart NY 30 600 9.99 5994
Joshua Newsom New_York Trader_Depot NY 90 800 7.99 6392
Edna Brock Raleigh Elliott's_Department_Stores NC 90 9200 1.88 17296
Edna Brock Raleigh Elliott's_Department_Stores NC 100 4400 1.98 8712
Michael Wiggum Los_Angeles Trader_Depot CA 100 600 2.5 1500
Joshua Newsom New_York Trader_Depot NY 90 600 2.5 1500
Edna Brock Raleigh Elliott's_Department_Stores NC 100 8800 1.68 14784
Heather Willis Atlanta Big_Chuck_Diners GA 100 200 4.99 998
Beth Munin Seattle Little_Corner_Sweets WA 100 200 2.49 498
Shirley Brightwell San_Francisco Pacific_Cafe_Company CA 100 1200 1.89 2268
Tim Kelly New_York Nuts_and_Things NY 90 14000 2.29 32060

Output( expected) this works only on Mac

#Name Surname City Amount
Tony Passaquale NJ Edenvale 3824
Wendy Leach NY New_York 1000
Craig Flanders NE Omaha 11388
Tony Passaquale NJ Edenvale 2290
Edna Brock NC Raleigh 25632
Ralph Khan NY New_York 5994
Joshua Newsom NY New_York 6392
Edna Brock NC Raleigh 17296
Edna Brock NC Raleigh 8712
Joshua Newsom NY New_York 1500
Edna Brock NC Raleigh 14784
Tim Kelly NY New_York 32060

The sum is all printed amounts is 130872

Output( on Ubuntu or CentOSX)

   #Name Surname City Amount
: integer expression expected
: integer expression expected
: integer expression expected
./script.sh: line 13: [: 9.99: integer expression expected
: integer expression expected
: integer expression expected
: integer expression expected
: integer expression expected
: integer expression expected
: integer expression expected
: integer expression expected
: integer expression expected
: integer expression expected
: integer expression expected
: integer expression expected
: integer expression expected
: integer expression expected
: integer expression expected
: integer expression expected
: integer expression expected
: integer expression expected
: integer expression expected
: integer expression expected
: integer expression expected
: integer expression expected
: integer expression expected
: integer expression expected
: integer expression expected
: integer expression expected
: integer expression expected
: integer expression expected
: integer expression expected
: integer expression expected
: integer expression expected

The sum is all printed amounts is 0
Braiam
  • 35,991
gkmohit
  • 3,309
  • What is the output now you are getting? Can you provide that information? – Thushi Jul 18 '14 at 04:53
  • @Learn could you see my edit :) – gkmohit Jul 18 '14 at 04:54
  • Yeah...How are you running your script?? Don't run like this sh script.sh filename.You should run ./script.sh filename. – Thushi Jul 18 '14 at 04:56
  • Yes, That is what I am doing. But it still doesn't work :( – gkmohit Jul 18 '14 at 04:57
  • I'm not able to reproduce that on ubuntu.I'm just getting the following output $ ./hero.sh he.txt #Name Surname Weapons City Amount – Thushi Jul 18 '14 at 05:03
  • First of all It is not satisfying the while condition. – Thushi Jul 18 '14 at 05:05
  • @Learn how about now? :) – gkmohit Jul 18 '14 at 05:06
  • @Unknown Is that Gowtham on purpose or should that be Gotham? – Anthon Jul 18 '14 at 05:09
  • 31 is not greater than 31, so your expected output shouldn't come out anywhere. Can you fill in the actual output from running the actual source from the post somewhere it works? At the moment they don't match at all. – Michael Homer Jul 18 '14 at 05:09
  • @Learn I have changed to complete question . . . – gkmohit Jul 18 '14 at 05:10
  • @Unknown Now it should work. – Thushi Jul 18 '14 at 05:10
  • 1
    @MichaelHomer changed the code completely. . . – gkmohit Jul 18 '14 at 05:11
  • 1
    @anthon Gowtham or Gotham, I guess that is nothing to do with that script.That is just a string – Thushi Jul 18 '14 at 05:11
  • @MichaelHormer Yes,Your changes should work for unknown.Up voted :) :D – Thushi Jul 18 '14 at 05:12
  • So why is that the output in CentOS or Ubuntu ? :/. . . Isn't bash universal :/ – gkmohit Jul 18 '14 at 05:13
  • `$ ./new_hero.sh he.txt #Name Surname City Amount Tony Passaquale NJ Edenvale 3824 Wendy Leach NY New_York 1000 Craig Flanders NE Omaha 11388 Tony Passaquale NJ Edenvale 2290 Edna Brock NC Raleigh 25632 Ralph Khan NY New_York 5994 Joshua Newsom NY New_York 6392 Edna Brock NC Raleigh 17296 Edna Brock NC Raleigh 8712 Joshua Newsom NY New_York 1500 Edna Brock NC Raleigh 14784 Tim Kelly NY New_York 32060

    The sum is all printed amounts is 130872` It is working on ubuntu!

    – Thushi Jul 18 '14 at 05:15
  • @Learn No way :/ It never worked it on the CentOS at school :/ wow – gkmohit Jul 18 '14 at 05:16
  • Again, I don't believe your data, script, and posted output correspond to one another. "[: : integer expression expected" means you have an empty string before -gt; the 9.99 message means you have 9.99 there. – Michael Homer Jul 18 '14 at 05:21

2 Answers2

4

If you see : integer expression expected, it's a sign that what's before the : ends with a carriage return character. A carriage return causes your terminal to overwrite the current line with the subsequent text, so if a field contains something like 1234␍ where is a carriage return, the shell displays the error message 1234␍: integer expression expected, and the 1234 is overwritten by : in.

You're getting a carriage return because your input file is a Windows text file and this is the last field on the line. To use the file under Linux or Cygwin, you need to convert it to a Unix text file. Windows text files use the two-character sequence CR-LF (carriage return, line feed) to mark the end of a line. Unix text files use the single character LF. So when Linux sees a Windows text file, it sees that each line is terminated by a CR character — which is a valid character, but rarely a desired one, and is not a valid character in an integer.

The message 9.99: integer expression expected shows that there's a line where 9.99 is in the 9th field. From your sample data it looks like this is expected in the 8th field, so you have a line with bad data (probably a spurious space one of the name fields).


Your script is very cumbersome. Don't check whether the argument is a regular file: this serves no useful purpose (the redirection will fail if the file doesn't exist) and makes it impossible to use a pipe as input. Don't use cut to parse fields: read can do it (assuming there are no empty fields). The || [[ -n $LINE ]] fragment doesn't do anything useful (but do make sure that your input is a valid text file; in a valid non-empty text file, the last character is LF). Use shell arithmetic instead of expr. As a general principle, use double quotes around variable substitutions (though here it won't matter with valid data — but consider what could happen if someone wrote * in a field). Untested rewrite:

#!/bin/bash
set -e
sum=0
echo "#Name Surname City Amount"
while read -r firstName lastName city f4 state f6 f7 f8 amount; do
    if [ "$amount" -gt 999 ] ; then
        case "$state" in
          N[YCEJ])
            echo "$firstName $lastName $state $city $amount"
            sum=$((sum + amount));;
        esac
    fi
done < "$1"
echo ""
echo "The sum is all printed amounts is $sum"
echo ""

This would be easier altogether as an awk script. Again, untested.

awk '
    $9 > 999 && $5 ~ /^[N[YCEJ]]$/ {
        print $1, $2, $5, $3, $9;
        sum += $9;
    }
    END { print "\nThe sum is all printed amounts is " sum }
' <"$1"
0
#!/bin/bash
if [ -f $1 ] ;
then
    sed 's/^M$//' $1 > $1.txt
    total=0
    amount=0
    echo "#Name Surname City Amount"
    while read -r LINE || [[ -n $LINE ]]; do

         firstName=$( echo $LINE | cut -d " " -f1)
            lastName=$( echo $LINE | cut -d " " -f2)
        city=$( echo $LINE | cut -d " " -f3)
        amount=$( echo $LINE | cut -d " " -f9)
        check=$( echo $amount | grep -c "[0-9]")
        if [ $check -gt 0 ]; then
           #echo $amount
           checkAmount=$( echo "$amount > 999" | bc)
           state=$(echo $LINE | cut -d " " -f5)
           correctState=$( echo $state | grep -c "^N[YCEJ]" )
            if [ $checkAmount -gt 0 ] &&  [ $correctState -gt 0 ]  ; then
                        echo "$firstName $lastName $state $city $amount"
                        total=`expr $total + $amount`
            fi
        fi
    done < $1
    echo ""
    echo "The sum is all printed amounts is $total"
    echo ""
    else
        echo "No file found"
fi

I just removed all the carriage return character( not sure ) and was able to make it work as required.

gkmohit
  • 3,309