When running if condition, getting the following error

Hi All,

My input file name is 1.sh

the contents of file are

cat status2.txt | while read filename
do
echo "$filename"
echo "first content of the file is ${filename[0]}"
echo "second content of the file is ${filename[0]}"
echo "second content of the file is ${filename[0]}"
if [ ${filename[0]} -eq 0 -a ${filename[1]} -eq 0 -a ${filename[2]} -eq 0 ]
then
echo "please execute the step 1, then step 2 and then step3"
elif [ ${filename[0]} -eq 0 ]
then
echo "please execute the step 2 and then step3"
elif [ ${filename[0]} -eq 0 -a ${filename[1]} -eq 0 -a ${filename[2]} -ne 0 ]
then
echo "please execute the last step3"
fi
done

Where status2.txt contains the following contents
0 --> first content is 0
0 --> Second content is 0
--> Third content is the space

Now running the 1.sh file i am getting the following output.

0
first content of the file is 0
second content of the file is 0
second content of the file is 0
./1.sh: [: too many arguments -------> Error
please execute the step 2 and then step3
0
first content of the file is 0
second content of the file is 0
second content of the file is 0
./1.sh: [: too many arguments -------> Error
please execute the step 2 and then step3

first content of the file is
second content of the file is
second content of the file is
./1.sh: [: too many arguments -------> Error
./1.sh: [: -eq: unary operator expected -------> Error
./1.sh: [: too many arguments -------> Error
dmadmin SYDUEND01 /tmp/A380_RFS24

can anybody tell me that why i am getting the above error??
It is very urgent, Please response me ASAP.

Thanks

Presumably ${filename[0]} gets expanded into an empty string, and test isn't able to parse that. Remember that unquoted empty strings will simply disappear. Either quote the strings, or append a character before them (or, to be on the safe side, do both -- that's why you frequently see something like if [ X"$string" = X ])

vnix$ echo $nosuchvariable


vnix$ [ $nosuchvariable -eq 0 -a $nosuchvariable -eq 0 -a $nosuchvariable -eq 0 ]
bash: [: too many arguments

vnix$ [ "$nosuchvariable" -eq 0 -a "$nosuchvariable" -eq 0 -a "$nosuchvariable" -eq 0 ]
bash: [: : integer expression expected

I don't understand why you expect $filename to turn into an array. Your while loop reads one line at a time, and the value you read replaces the previous value for $filename. If you want it to be an array, you need to initialize it in a different way.

Maybe you really mean something like this.

exec <status.txt
read file1
read file2
read file3

echo "first content of the file is $file1"
echo "second content of the file is $file2"
echo "third content of the file is $file3"  # or do you really mean to print "second" twice?
if [ "$file1" -eq 0 -a "$file2" -eq 0 -a "$file3" -eq 0 ]
then
echo "please execute the step 1, then step 2 and then step3"
elif [ "$file1" -eq 0 ]
then
echo "please execute the step 2 and then step3"
elif [ "$file1" -eq 0 -a "$file2" -eq 0 -a "$file3" -ne 0 ]
then
echo "please execute the last step3"
fi

This will still print an error message if not all the input is numeric (that's the "integer expression expected" case in the example above). You might be better off using a case statement instead; it's much less sensitive to the type of input etc than test (aka "[").