Tag Archives: [: -ge/-le/=/… : unary operator expected (standard_in) 1: syntax error

[Solved] Shell Script [: -ge/-le/=/… : unary operator expected (standard_in) 1: syntax error

There was a syntax error when writing the script, but the result was correct

Error reporting behavior in script:

for (( i=0; i<=$ line1; i=i+1 ))
do
if [ $(echo “${R12S[i]} < 3” |bc) -eq 1 ] && amp; [ $( echo “${R23S[i]} > 3″|bc) -eq 1 ]\
&& amp; [ $( echo “${R13S[i]} > 3″|bc) -eq 1 ]; then
P1=P1+1
elif [ $(echo “${R12S[i]} > 3” |bc) -eq 1 ] && amp; [ $( echo “${R23S[i]} > 3” |bc) -eq 1 ]\
&& amp; [ $( echo “${R13S[i]} < 3″|bc) -eq 1 ]; then
P2=P2+1
elif [ $(echo “${R12S[i]} > 3” |bc) -eq 1 ] && amp; [ $( echo “${R23S[i]} < 3” |bc) -eq 1 ]\
&& amp; [ $( echo “${R13S[i]} > 3” |bc) -eq 1 ]; then
P3=P3+1
elif [ $(echo “${R12S[i]} > 3” |bc) -eq 1 ] && amp; [ $( echo “${R23S[i]} > 3” |bc) -eq 1 ]\
&& amp; [ $( echo “${R13S[i]} > 3” |bc) -eq 1 ]; then
P4=P4+1
fi
done

The error is as follows:

(standard_ in) 1: syntax error
path.sh: line 84: [: -eq: unary operator expected
(standard_ in) 1: syntax error
path.sh: line 87: [: -eq: unary operator expected
(standard_ in) 1: syntax error
path.sh: line 90: [: -eq: unary operator expected
(standard_ in) 1: syntax error
path.sh: line 93: [: -eq: unary operator expected

Use the SH – C path.sh command to check whether there are syntax errors in the script
or not

However, there is no error, indicating that the script itself is not a problem, the problem should be in the run time

Baidu reported an error [: – EQ: unary operator expected], some netizens said it was because the condition after if had to be double []

So all the conditional statements were modified, but the error was still reported

(standard_ in) 1: syntax error

(standard_ in) 1: syntax error

(standard_ in) 1: syntax error

(standard_ in) 1: syntax error

Later, I found that it was the problem of circulation

for (( i=0; i<=$ line1; This cycle starts from 0 and ends at $Line1, which is equivalent to $Line1 + 1 cycles

However, there is only $Line1 number in the array, so one number is missing, so an error will occur. This also proves why the running result is correct, because the last number is empty, which will not affect the previous number

Modify: change for ((I = 0; i<=$ line1; I = I + 1) is changed to for ((I = 0; i<=$ line1-1; If I = I + 1), there will be no error message when running again

After checking the information again, it is found that many people encounter this type of error reporting:

[: – Ge/- Le/= /…: unary operator expected error

Error reason:

Since the initialization value of the variable rate is empty, it becomes [- Ge “10”]. Obviously [is not compared with “10” and lacks [symbol], so this error is reported

Solution:

1. Check whether the assignment is null due to the wrong writing of the assignment statement

2. Add declare – I rate = 0 before assignment

3. Change it to if [[$rate – Ge 10]] and add a pair of []