Read Variable From Fille And Convert it to Integer

I read 3 variables from from Inputfile.txt the third one "startnumber" is a number when i compare it with 9 ($startnumber -le 9) it give's me a "unary operator expected", i know that -le is for number comparison. What i need is to convert $startnumber to integer (i have try to do it with expr but no luck)(I'm using Fedora 14). Thanks in advance.

#!/bin/sh
file="Inputfile.txt"
while IFS=\| read fullname spacename startnumber
do
....
if [ $startnumber -le 9 ];
....

A number as far as the shell is concerned, a number is a string that holds digits. You don't need to "convert" it, something else is going on.

Could you post a sample of your data file?

1 Like
FileName1|File Extension 1|3
FileName2|File Extension 2|100
FileName3|File Extension 3|545

Works fine.

Keep in mind that any blank line or record will provoke that error. You might want to put an [ -z "$fullname" ] && continue at the top of your loop to ignore blank lines.

Also keep in mind that carriage returns will foul it up. If you edited that file in windows, it will be full of them. tr -d '\r' < infile > fixedfile to get rid of them. Depending on your shell, you could also just change IFS into IFS="|"$'\r' to tell read that carriage returns are whitespace and it shouldn't go bananas on them.

1 Like

Still no luck i get the error
"-le: unary operator expected"
at the if statement

#!/bin/sh

file="inputfile.txt"

while IFS='|' read fullname spacename startnumber
do
echo "$fullname  $spacename  $startnumber"

if [ "$startnumber" -le 9 ];then
  echo "startnumber is less than or equal to 9"
fi
done < $file

exit 0

Output:

$ test
FileName1  File Extension 1  3
startnumber is less than or equal to 9
FileName2  File Extension 2  100
FileName3  File Extension 3  545

$

Of course you need some error handling and data validating. If you are expecting a number for comparison you should test to be sure its a number first. Also beware of "magic numbers" (the number 9 in the test. What is 9, a count, a range, a code for something else?). Make a variable for 9 at the top with a meaningful name, so if it ever has to change you only have one place to edit, plus the meaningful name makes the code easier to understand and maintain for the guy after you:

#!/bin/sh
WARNSTARTNUMBER=9    # Print warning if startnumber is <= this value.
file="inputfile.txt"

while IFS='|' read fullname spacename startnumber
do
echo "$fullname  $spacename  $startnumber"

if [ "$startnumber" -le $WARNSTARTNUMBER ];then
  echo "startnumber is less than or equal to $WARNSTARTNUMBER"
fi
done < $file

exit 0
1 Like

Could you attach your data file? The actual file, not what you think it looks like. I think it's not what you expect.

1 Like

It looks better now, it gives me this error only when the data file has only one line (the end of line is the end of file).

I suspect this is because of what I warned you about earlier, blank lines... But I'll never be sure until you actually post the file...