Shell script, if judgment statement error [: Too many arguments
I have encountered two situations:
1, the first situation is that the Internet, you can say, the string variable may exist in a space, shell resolution when it is considered to be more than one parameter, and then judge, you can not know what to get the value, for example:
Script ee.sh:
strings1= "Hello World" strings2= "Hello World"
if [$strings 1 = $strings 2]
Then
echo "STRINGS1 is same as STRINGS2"
Else
echo "STRINGS1 is different from STRINGS2"
Fi
Error in execution result:
Ee.sh:line 3: [: Too many arguments
STRINGS1 is different from STRINGS2
and automatically executes the statements in the Else branch.
The following statement would not be like this:
Script ee.sh:
strings1= "Hello World"
strings2= "Hello World"
If ["$strings 1" = "$strings 2"]
Then
echo "STRINGS1 is same as STRINGS2"
Else
echo "STRINGS1 is different from STRINGS2"
Fi
Execution result: STRINGS1 is same as Strings2
2, the second is my initial contact with the shell script, a lot of grammar rules are not familiar with the encounter, I write it as-multiple judgments:
Or, in the example above, change the second paragraph slightly:
Script ee.sh:
strings1= "Hello World"
strings2= "Hello World"
If ["$strings 1" = "$strings 2"-eq 1]
Then
echo "STRINGS1 is same as STRINGS2"
Else
echo "STRINGS1 is different from STRINGS2"
Fi
Execution results:
Ee.sh:line 3: [: Too many arguments
STRINGS1 is different from STRINGS2
This is written in the usual way, "$strings 1" = "$strings 2" to determine whether two strings are equal, and if the result of equality is true, and true is usually equivalent to 1 in the general language, and therefore compares this result with 1 ", In fact, in this logic, I ignore the "$strings 1" = "$strings 2" itself is a judgment condition, the execution result is true or false, without the need to judge the result whether it is 1.