If the shell determines that a variable is empty

Source: Internet
Author: User

1. The most straightforward and straightforward judgment

Without those if statements, directly shorten the amount of code.

2. The variable is caused by "" quotation marks

As shown below:, you can get the result is NULL.

#!/bin/sh  a=  if [!-n "$a"]; then  echo ' is null '  else  echo ' NOT NULL '  

 

3. Judging directly by variables

The results are as follows: Is NULL, the same as the first method, but the code is a little longer, it is recommended to use the second way of judging, simple and clear.

#!/bin/sh  a=  if [! $a]; then echo ' is null '  else  echo ' NOT NULL '  

4. Use test to determine

The results are: A is not set!

#!/bin/sh  a=  if test-z "$a" then  echo "A was not set!"  else  echo "A is set!"  

5. Use "" to determine

#!/bin/sh  a=  If ["$a" = ""]; Then  echo "A was not set!"   else  echo "A is set!"  

 

This situation is easy to make the foot of the newspaper wrong

Transferred from: http://luxiaok.blog.51cto.com/2177896/925548

 

  

  

 

If the shell determines that a variable is empty

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.