test -n和test -z都为真的情况 [英] A case in which both test -n and test -z are true

查看:36
本文介绍了test -n和test -z都为真的情况的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#! /bin/bash

echo "Please input 2 nums: "

read a b

if [ -z $b ]; then
        echo b is zero !
fi

if [ -n $b ]; then
        echo b is non-zero !
fi

当运行脚本时,仅输入1个数字,而另一个则保留为空,则b应该为空.但结果是同时打印了两个回声.

when run the script, only input 1 number, and leave the other empty, then b is supposed to be null. but the result is both echo is printed.

-laptop:~$ ./test.sh 
Pleaes input 2 nums: 
5 
b is zero !
b is non-zero !

b既为空又为非空?有人可以对此发表评论吗?谢谢!

b is both null and non-null ?! Could anyone comment on this ? Thanks !

推荐答案

替换

if [ -z $b ]; then

使用

if [ -z "$b" ]; then

并在其他 if 条件下也进行同样的操作.

And do the same in the other if condition as well.

请参见 http://tldp.org/LDP/abs/html/testconstructs.html进行一些有趣的测试.

See http://tldp.org/LDP/abs/html/testconstructs.html for some interesting tests.

这篇关于test -n和test -z都为真的情况的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆