Get the App
SLTechnology News&Howtos  ›  Servers  › 

Summary of shell method to determine whether a variable is empty or not

Shulou Source: shulou.com Published: 2022-06-02 03:09:35 11月07日 Update

How to determine whether a variable is empty in shell

In shell programming, the error check item of a parameter includes whether a variable is assigned (that is, whether a variable is empty). The method to determine whether a variable is empty is as follows:

1. Variables are enclosed in quotation marks

#! / bin/shpara1=if [!-n "$para1"]; then echo "IS NULL" else echo "NOT NULL" fi

[output result] "IS NULL"

two。 Judge directly by variables

#! / bin/shpara1=if [! $para1]; then echo "IS NULL" else echo "NOT NULL" fi

[output result] "IS NULL"

3. Use test to judge

#! / bin/shdmin=if test-z "$dmin" then echo "dmin is not set!" else echo "dmin is set!" fi

[output result] "dmin is not set!"

4. Use "" to judge

#! / bin/sh dmin=if ["$dmin" = ""] then echo "dmin is not set!" else echo "dmin is set!" fi

[output result] "dmin is not set!"

Tags: Variables results outputs parameters quotes empty squares errors checks programming methods Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS Microsoft Shulou Technology MariaDB MySQL