Home > php教程 > PHP开发 > Solution to too many arguments problem in linux bash

Solution to too many arguments problem in linux bash

高洛峰
Release: 2017-01-04 17:23:47
Original
2080 people have browsed it

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`"
Copy after login

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.

For more related articles on the solution to the too many arguments problem in linux bash, please pay attention to the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template