When the script is executed, the following error appears: “h.sh: Line 9: [: =: unary operator expected”. The specific script is as follows:
[root@Dasoncheng sbin]# cat -n h.sh
1 #!/bin/bash
2 sh -n $1 &>/tmp/err
3 if [ $?-eq 0 ];
4 then
5 echo "The script is ok"
6 else
7 cat /tmp/err
8 read -p "Please input q/Q to exit , or others to edit it by vim: " n
9 if [ $n == "q" ] || [ $n == "Q" ];
10 then
11 exit
12 else
13 vim $1
14 exit
15 fi
16 fi
[root@Dasoncheng sbin]# sh h.sh i.sh
i.sh: line 3: syntax error: unexpected end of file
Please input q/Q to exit , or others to edit it by vim:
h.sh: line 9: [: ==: unary operator expected
h.sh: line 9: [: ==: unary operator expected ##This is the error message that is executed.
Solution:
if [ $n == "q" ] || [ $n == "Q" ];change to :if [ "$n" == "q" ] || [ "$n" == "Q" ];
##Done!
Because when $n is null (i.e. carriage return does not input any parameters), it becomes [= = q “], which obviously lacks the comparison parameter. When the value of $n is not empty, the script is normal
it can also be avoided by using the following methods:
if [[ $n == "q" ]] || [[ $n == "Q" ]]
##or
if [ "$n"x == "q"x ]
##where x can also be other characters.
Similar Posts:
- [Solved] Shell Script [: -ge/-le/=/… : unary operator expected (standard_in) 1: syntax error
- Shell script error: ‘[: =: unary operator expected’
- [Solved] Operator overload must take either zero or one argument error
- String null space error: binary operator expected
- Conversion to MySQL shell script execution error $’\ R’: command not found
- Spring CONTROLLER Servlet.service() for servlet jsp threw exception
- error: no match for ‘operator<' (operand types are 'const Request_Info' and 'const Request_Info')xxxxxxx
- Solutions to the problem of too many arguments in Linux Bash
- Shell script error newline syntax error near unexpected token
- How to Solve JAVA Error: “error: bad operand types for binary operator ”