Solution to too many arguments problem in Linux bash _linux shell

Source: Internet
Author: User

Determine if the contents of a file are not empty, use the statement:

If test-z ' cat filename '

When the filename is empty or there is only one line of strings without spaces, all normal, on the contrary, will report: Too many arguments, or even: binary operator and other errors.

Reference article: http://www.ibm.com/developerworks/cn/linux/shell/bash/bash-2/index.html

Reason Analysis:
FileName in the space to enter and so confuse bash.
If the environment variable is not enclosed in double quotes, Bash considers the arguments in the condition to be too many.

You can enclose a string argument in double quotes to eliminate the problem.

It seems to be the habit of enclosing all string arguments in double quotes will remove many of the same programming errors.

Workaround:

Add double quotes to the command execution section:

Copy Code code as follows:

If Test-z "' Cat filename '"


The solution of the shell's too many arguments two:

Today, occasionally because of solving a small problem, encountered a strange problem "[]" and "[[]]" The different effects, summed up as follows

Show CODE:IF [-Z ' lsof-i:22 ']///This style will be reported too many arguments, changed to [[Z ' lsof-i:22 ']]
Then
echo "The port is not running"
Else
echo "The port is running"
Fi

When you run this simple shell script, you always report too many arguments, finally found and version of the universality is related to, more simply, "[[]]" than "[]" has a better versatility, so, in order to avoid this problem, directly to the "[]" Replace to "[[]] "It's okay. and "[[]]" is more fault tolerant than "[]", that is, the "[]" possible error in the [[]] structure may not be an error, such as &&,| | Such a logical judgment, because it is not the focus of this area and passed.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.