#!/bin/bash
# testing_condition.sh
echo "Testing \"0\""
if [ 0 ]
then
echo "0 is true."
else
echo "0 is false."
fi # output -> 0 is true.
echo
echo "Testing \"1\""
if [ 1 ]
then
echo "1 is true."
else
echo "1 is false."
fi # output -> 1 is true.
echo
echo "Testing \"-1\""
if [ -1 ]
then
echo "-1 is true."
else
echo "-1 is false."
fi # output -> -1 is true.
echo
echo "Testing \"NULL\""
if [ ]
then
echo "NULL is true."
else
echo "NULL is false."
fi # output -> NULL is false.
echo
echo "Testing \"xyz\""
if [ xyz ]
then
echo "Random string is true."
else
echo "Random string is false."
fi # output -> Random string is true.
echo
echo "Testing \"\$xyz\""
if [ $xyz ]
then
echo "Uninitialized variable is true."
else
echo "Uninitialized variable is false."
fi # output -> Uninitialized variable is false.
echo
echo "Testing -n \"-n \$xyz\""
if [ -n "$xyz" ]
then
echo "Uninitialized variable is true."
else
echo "Uninitialized variable is false."
fi # output -> "Uninitialized variable is false."
echo
xyz=
echo "Testing \"-n \$xyz\""
if [ -n "$xyz" ]
then
echo "Null variable is true."
else
echo "Null variable is false."
fi # output -> Null variable is false.
echo
echo "Testing \"false\""
if [ "false" ]
then
echo "\"false\" is true."
else
echo "\"false\" is false."
fi # output -> "false" is true.
echo
echo "Testing \"\$false\""
if [ "$false" ]
then
echo "\"\$false\" is true."
else
echo "\"\$false\" is false."
fi # output -> "$false" is false.
echo
echo "Testing \"\$true\""
if [ "$true" ]
then
echo "\"\$true\" is true."
else
echo "\"\$true\" is false."
fi
exit 0
shell脚本测试各种参数类型的非空(真)
于 2023-11-24 09:02:15 首次发布