Summary of shell method to determine whether a variable is empty or not
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!"