为什么此脚本在if语句中执行字符串:
#!/bin/bash
FILES="*"
STRING=''
for f in $FILES
do
if ["$STRING" = ""]
then
echo first
STRING='hello'
else
STRING="$STRING hello"
fi
done
echo $STRING
用sh script.sh输出时运行它:
first
lesscd.sh: line 7: [hello: command not found
lesscd.sh: line 7: [hello hello: command not found
lesscd.sh: line 7: [hello hello hello: command not found
lesscd.sh: line 7: [hello hello hello hello: command not found
lesscd.sh: line 7: [hello hello hello hello hello: command not found
hello hello hello hello hello hello
附:首次尝试shell脚本
谢谢
解决方法:
您正在尝试执行命令[hello.在之后放置一个空格[因此它将被认定为测试.
for f in $FILES
do
if [ "$STRING" = "" ]
then
echo first
STRING='hello'
else
STRING="$STRING hello"
fi
done