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


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章