bash 中的 [ test ] 和 [[ test ]] 有什么区别?什么时候一个比另一个更合适,;最后的作用是什么?
if [[ -z $DIRECTORY ]];
then
DIRECTORY=html
fi
if [ ! -d "$DIRECTORY" ]; then
echo installation directory "'${DIRECTORY}'" does not exist
exit 1
fi
bash 中的 [ test ] 和 [[ test ]] 有什么区别?什么时候一个比另一个更合适,;最后的作用是什么?
if [[ -z $DIRECTORY ]];
then
DIRECTORY=html
fi
if [ ! -d "$DIRECTORY" ]; then
echo installation directory "'${DIRECTORY}'" does not exist
exit 1
fi
[[是一个与命令类似(但比[命令更强大)的 bash 关键字。请参阅http://mywiki.wooledge.org/BashFAQ/031和http://mywiki.wooledge.org/BashGuide/TestsAndConditionals除非您正在编写 POSIX sh,否则我们建议使用[[.
我们通常在以下情况下使用单方括号:
if [ -L $file ]; thenif [ $a -lt $b ]; then" "像正常一样使用和处理特殊字符(例如星号):if [ -z "$string" ]; then当我们:
if [[ "$string1" == *[sS]tring* ]]; thenif [[ -a *.sh ]]; thenif [[ $a == 3 || $b == 4]]; then" "[用于外壳,[[用于 bash。
例如:
Try [ $A -eq 1 ]: 如果$A未设置,则会引发错误。
[[ $A -eq 1 ]]将起作用。