linux shell 中判断字符串为空的正确方法
生活随笔
收集整理的這篇文章主要介紹了
linux shell 中判断字符串为空的正确方法
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
help命令可以查看幫助
help test
?
正確做法:
?
#!/bin/sh
STRING=
if [ -z "$STRING" ]; then
??? echo "STRING is empty"
fi
if [ -n "$STRING" ]; then
??? echo "STRING is not empty"
fi
?
root@james-desktop:~# ./zerostring.sh
STRING is empty
-------------------------------------------------------------------------
錯誤做法:
#!/bin/sh
STRING=
if [ -z $STRING ]; then
??? echo "STRING is empty"
fi
if [ -n $STRING ]; then
??? echo "STRING is not empty"
fi?
輸出錯誤結果:
root@james-desktop:~# ./zerostring.sh
STRING is empty
STRING is not empty
總結
以上是生活随笔為你收集整理的linux shell 中判断字符串为空的正确方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux shell 自定义函数(定义
- 下一篇: 设置 Linux 的 LD_LIBRAR