字符串比较

时间:2019-04-16 23:59:04  来源:igfitidea点击:

shell判断字符串是否相等

语法

STRING1 = STRING2

示例

#!/bin/bash
pass="theitroad"
echo 
if test "$pass" = "theitroad"
then
    echo "password is correct!"
fi

shell判断字符串是否不相等

语法

STRING1 != STRING2

示例

#!/bin/bash
pass="theitroad"
echo 
if test "$pass" != "theitroad"
then
    echo "wrong password!"
fi

shell判断字符串长度是否为0

语法

-z STRING

示例

#!/bin/bash
read -s -p "请输入密码 " pass
echo 
if test -z $pass 
then
    echo "密码不能为空!"	
    exit 1
fi