您的问题是缺少换行符(或者,在极端情况下,分号)。你的脚本是:
for file in baselines/*; do numThreads=$(echo $file | gawk 'BEGIN { FS = "." } ; {print $3 }') echo "Number of Threads: $numThreads" >> $file; done
您需要在echo "Number of Threads..."
部件之前使用换行符或分号。像这样为了可读性:
for file in baselines/*
do
numThreads=$(echo $file | gawk 'BEGIN { FS = "." } ; {print $3 }')
echo "Number of Threads: $numThreads" >> $file
done
如果你坚持编写不可读的代码,就像这样:
for file in baselines/*; do numThreads=$(echo $file | gawk 'BEGIN { FS = "." } ; {print $3 }'); echo "Number of Threads: $numThreads" >> $file; done
但请不要再将这样的代码发送到 Stack Overflow 进行审查/修订,因为它很难阅读,人们不会费心回答难以阅读代码的问题。
顺便说一句,您的代码所做的事情是非常微妙的错误。它在echo
环境变量numThreads
设置为正确值的情况下运行命令,但参数列表的扩展发生在此之前,因此在评估字符串时它numThreads
是空的。"Number of Threads: $numThreads"
证明这一点的一种方法是:
for file in baselines/*
do
numThreads=$(echo $file | gawk 'BEGIN { FS = "." } ; {print $3 }') env | grep numThreads
done
这将打印环境变量的值。另一种证明方式是:
for file in baselines/*
do
numThreads=$(echo $file | gawk 'BEGIN { FS = "." } ; {print $3 }') sh -c 'echo numThreads=$numThreads'
done