if 语句在 Bash 脚本中不起作用
if statement not working in Bash script
我不明白为什么这个检查字符串是否为空的简单 if
语句不起作用。它给了我:
syntax error near unexpected token `then'
#!/bin/bash
str="Hello World!"
if[ ! -z "$str"]; then
echo "str is not empty"
fi
我在我的 PC 上和 online editor 上都试过了,但它显示了同样的问题。有什么想法吗?
#!/bin/bash
str="Hello World!"
if [ ! -z "$str" ]; then
echo "str is not empty"
fi
也许,
在 "if" 语句之后插入 space。
并且,在“]”之前。
[[ ]]
在测试方面优于 [ ]
:
#!/bin/bash
str="Hello World!"
if [[ ! -z $str ]]; then
echo "str is not empty"
fi
缩写形式:
#!/bin/bash
str="Hello World!"
[[ ! -z $str ]] && echo "str is not empty"
我不明白为什么这个检查字符串是否为空的简单 if
语句不起作用。它给了我:
syntax error near unexpected token `then'
#!/bin/bash
str="Hello World!"
if[ ! -z "$str"]; then
echo "str is not empty"
fi
我在我的 PC 上和 online editor 上都试过了,但它显示了同样的问题。有什么想法吗?
#!/bin/bash
str="Hello World!"
if [ ! -z "$str" ]; then
echo "str is not empty"
fi
也许, 在 "if" 语句之后插入 space。 并且,在“]”之前。
[[ ]]
在测试方面优于 [ ]
:
#!/bin/bash
str="Hello World!"
if [[ ! -z $str ]]; then
echo "str is not empty"
fi
缩写形式:
#!/bin/bash
str="Hello World!"
[[ ! -z $str ]] && echo "str is not empty"