shell条件判断 条件为何值时判断语句为真


#!/bin/bash

if [ 0 ]
then
    echo "0 is true"
else
    echo "0 is flase"
fi

if [ 1 ]
then
    echo "1 is true"
else
    echo "1 is flase"
fi

if [ -1 ]
then
    echo "-1 is true"
else
    echo "-1 is flase"
fi

if [ 10 ]
then
    echo "10 is true"
else
    echo "10 is flase"
fi

if [ ]
then
    echo "NULL is true"
else
    echo "NULL is flase"
fi

if [ xyz ]
then
    echo "xyz is true"
else
    echo "xyz is flase"
fi

xyz=
if [ -n "$xyz" ]
then
    echo "$xyz is true"
else
    echo "\$xyz is flase"
fi

exit 0

# ./test.sh   输出判断
0 is true
1 is true
-1 is true
10 is true
NULL is flase
xyz is true
$xyz is flase


你可能感兴趣的:(shell脚本)