预期整数表达式的解决方案

预期整数表达式的解决方案

因此,当我在 Mac 上运行此代码时,没有任何错误,并且它为我提供了完美的输出。但是当我在 Ubuntu 或 CentOS 上运行它时,出现以下错误 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

输入文件:

#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

输出(预期)仅适用于 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

输出(在 Ubuntu 或 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

答案1

如果您看到: integer expression expected,则表明前面的内容:回车特点。回车符会导致终端用后续文本覆盖当前行,因此如果字段包含类似1234␍where是回车符的内容,shell 将显示错误消息1234␍: integer expression expected,并且1234被 覆盖: in

您会收到回车符,因为您的输入文件是 Windows 文本文件,并且这是该行的最后一个字段。要在Linux或Cygwin下使用该文件,需要将其转换为Unix文本文件。 Windows 文本文件使用两个字符序列 CR-LF(回车、换行)来标记行的结尾。 Unix 文本文件使用单个字符 LF。因此,当 Linux 看到 Windows 文本文件时,它会发现每一行都以 CR 字符结尾——这是一个有效字符,但很少是所需的字符,并且不是整数中的有效字符。

该消息显示第 9 字段中9.99: integer expression expected有一行。9.99从您的示例数据来看,这似乎是在第 8 个字段中预期的情况,因此您的一行包含错误数据(可能是名称字段之一的虚假空格)。


你的脚本太麻烦了。不要检查参数是否是常规文件:这没有任何用处(如果文件不存在,重定向将失败)并且无法使用管道作为输入。不要用于cut解析字段:read可以做到(假设没有空字段)。该|| [[ -n $LINE ]]片段不会做任何有用的事情(但请确保您的输入是有效的文本文件;在有效的非空文本文件中,最后一个字符是 LF)。使用 shell 算术代替expr.作为一般原则,在变量替换周围使用双引号(尽管这里有效数据并不重要 - 但请考虑如果有人*在字段中写入会发生什么)。未经测试的重写:

#!/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 ""

作为 awk 脚本,这会更容易。再次,未经测试。

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"

答案2

#!/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

我刚刚删除了所有回车符(不确定)并能够使其按要求工作。

相关内容