Linux shell if [ -n ] 正确使用方法
生活随笔
收集整理的這篇文章主要介紹了
Linux shell if [ -n ] 正确使用方法
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
if [ str1 = str2 ] ?當兩個串有相同內容、長度時為真?
if [ str1 != str2 ] 當串str1和str2不等時為真?
if [ -n str1 ] 當串的長度大于0時為真(串非空)?
if [ -z str1 ] 當串的長度為0時為真(空串)?
if [ str1 ] 當串str1為非空時為真
shell 中利用 -n 來判定字符串非空。
錯誤用法:
ARGS=$*
if [ -n $ARGS ?]
then
? ?print "with argument"
fi
print " without argument"
不管傳不傳參數,總會進入if里面。
原因:因為不加“”時該if語句等效于if [ -n ],shell 會把它當成if [ str1 ]來處理,-n自然不為空,所以為正。
?
正確用法:需要在$ARGS上加入雙引號,即"$ARGS".
?
ARGS=$*
if [ -n "$ARGS" ?]
then
? ?print "with argument"
fi
print " without argument"
轉載于:https://www.cnblogs.com/dyh004/p/6347226.html
總結
以上是生活随笔為你收集整理的Linux shell if [ -n ] 正确使用方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 运维之我的nginx短篇教程
- 下一篇: Redux概念之一: Redux简介