为什么这个脚本在if语句中执行string:
#!/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
ps首先尝试一个shell脚本
谢谢
你正试图执行命令[hello
。 在[
所以它将被认为是一个考验。
for f in $FILES do if [ "$STRING" = "" ] then echo first STRING='hello' else STRING="$STRING hello" fi done
假设“echo first”这一行仅仅是为了调试,你可以用下面的方法实现:
STRING = $ STRING $ {STRING:+}你好
(也就是说,上面的行会产生和if语句相同的结果,但是不会首先回显)
如果$ STRING为空或为空,表达式“$ {STRING:+}”的计算结果为空,否则计算结果为空格。