需求描述: 今天帮同事调整脚本,涉及到判断一个字符串为非空的,在此记录下. 操作过程: 通过-n来判断字符串是否为非空,如果为非空那么就是真 #!/bin/bash Str1='MyTest' if [[ -n $Str1 ]];then echo "$Str1 is not empty." fi 执行结果: [am@hadoop3 scripts]$ sh test.sh MyTest is not empty. 备注:经过测试,字符串不是空,判断是真了. 文档创建时间:2018年4
1.字符串参与判断时:非空即为真判断字符串为空的方法if(str!=null && str!=undefined && str !='')可简写为if(!str){ console.log(str)}2.数字参与if判断:非0非NAN即为真 var i = 0;if(i){ alert('here');}else{ alert('test is ok!');} 输出结果为here var i = 0;if(i){ alert('here');}else{ alert(