Solution to integer expression expected
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"
Related videos on Youtube
![gkmohit](https://i.stack.imgur.com/tfScI.jpg?s=256&g=1)
gkmohit
I am an Entrepreneur, Web Designer and Online Business Consultant. My mission is to help small businesses grow by leveraging the power of the internet. I believe in automating tasks by using tools so that you can focus on your core business. I have always been a curious person. The first time I used a computer was in grade 8 and fascinated by how you could create digital art using Corel Draw. In class 10, I had the opportunity to use the first mobile phone, and I was very intrigued by how the OS integrated with the hardware. That same curiosity led me to write my first piece of code in grade 10, and I then realized the power a programmer had in this world. In the mid-2011 family and I moved from Bangalore, India to Toronto, Canada, where I started my undergraduate degree in Computer Science at York University. As a student, I couldn't wait to get some industry experience, and I was fortunate to land my first job in IT at the University Information Technology department. I started as a Technical Analyst and slowly grew to be a software developer at the Student Information System. Gaining some industry experience gave me the confidence to go and attend a few hackathons across North America. I was fortunate to win a few awards from companies like Google, IBM, Bank of Nova Scotia and more while attending hackathons. With the help of my awards, experience and my skills, I started my internship at SAP Labs in Waterloo, Canada. My course was great, but I was seeking something more challenging, so my hackathon team members and I decided to start a fast-growing development shop Hyfer Technologies. At Hyfer Technologies, I stumbled upon Product Management and Business Analysis while managing a team of developers remotely. So far, I have been able to work with 10+ clients from conception to production. As a product manager, I have had a few failed projects but also some that are still growing strong. As of March 2020, I am working with The Ottawa Hospital as a Business Analyst. As a Product Manager & Business Analyst, my skills include but are not limited to: Management Strategy Growth Strategy Customer, partner and client relations, Organizational Design Process Improvements Statistical Analysis and Data Mining Marketing and Brand Strategy Running Product-Related Sessions Managing technical team Through these skills and experience, I am confident I can add a lot of values to any growing team. I am always open to learning more about you and your business. Feel free to reach out to me or follow me on LinkedIn.
Updated on September 18, 2022Comments
-
gkmohit over 1 year
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
-
Thushi almost 10 yearsWhat is the output now you are getting? Can you provide that information?
-
gkmohit almost 10 years@Learn could you see my edit :)
-
Thushi almost 10 yearsYeah...How are you running your script?? Don't run like this
sh script.sh filename
.You should run./script.sh filename
. -
gkmohit almost 10 yearsYes, That is what I am doing. But it still doesn't work :(
-
Thushi almost 10 yearsI'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 almost 10 yearsFirst of all It is not satisfying the
while
condition. -
gkmohit almost 10 years@Learn how about now? :)
-
Anthon almost 10 years@Unknown Is that Gowtham on purpose or should that be Gotham?
-
Michael Homer almost 10 years31 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.
-
gkmohit almost 10 years@Learn I have changed to complete question . . .
-
Thushi almost 10 years@Unknown Now it should work.
-
gkmohit almost 10 years@MichaelHomer changed the code completely. . .
-
Thushi almost 10 years@anthon Gowtham or Gotham, I guess that is nothing to do with that script.That is just a
string
-
Thushi almost 10 years@MichaelHormer Yes,Your changes should work for unknown.Up voted :) :D
-
gkmohit almost 10 yearsSo why is that the output in CentOS or Ubuntu ? :/. . . Isn't bash universal :/
-
Thushi almost 10 years
$ ./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! -
gkmohit almost 10 years@Learn No way :/ It never worked it on the CentOS at school :/ wow
-
Michael Homer almost 10 yearsAgain, 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
; the9.99
message means you have9.99
there.
-