To determine whether the content of a file is empty, use the statement:
if test -z `cat filename`
When filename is empty or has only one line of string without spaces , everything is normal, otherwise, errors such as: too many arguments, or even: binary operator expected will be reported.
Cause analysis:
Spaces and carriage returns in filename confuse bash.
If the environment variable is not placed in double quotes, bash will think that there are too many independent variables in the condition.
This problem can be eliminated by enclosing the string argument in double quotes.
It seems that developing the habit of enclosing all string arguments in double quotes will eliminate many similar programming errors.
Solution:
Just add double quotes to the execution part of the command:
if test -z "`cat filename`"
Solution 2 of too many arguments in shell:
Today Occasionally, to solve a small problem, I encountered a strange problem. "[ ]" and "[[ ]]" have different effects. The summary is as follows
Display the code: If [ -z ` lsof -i :22 ` ] //This way of writing will report too many arguments, change to [[ -z ` lsof -i:22 ` ]]
else
Echo "The port is running"
FI
## When running this simple shell script, it always reports to Many Arguments. It is said that "[[ ]]" has better versatility than "[ ]", so in order to avoid this problem, just replace "[ ]" with "[[ ]]". Moreover, "[[ ]]" is more fault-tolerant than "[ ]", that is, errors that may be reported in "[ ]" may not be reported in "[[ ]] structures, such as &&, ||, etc. This kind of logical judgment is not the focus of this article, so I will pass it by.