我正在尝试运行一个循环,该循环将通过以下方式在后台运行matlab命令:
while [$i -lt $p] || [$i -eq $p] do
$i>i.txt
echo "matlab -nojvm -r readtxt_abc;pause(1);myfunc1(a,b,c);clear" |sh &
let i=i+1
done
我得到一个错误
bash:syntax error near unexpected token 'done'
如果有人知道错误的原因是什么,请让我知道,谢谢!
最佳答案
方括号本质上是命令而不是语法元素,因此它们之间需要有空格。将do
与while
放在同一行时,您还需要分号。
while [ $i -lt $p ] || [ $i -eq $p ]; do
小于等于运算符是
-le
,而不是执行两个测试。但是,这在Bash中是首选的:
while ((i <= p)); do
# $i>i.txt # this line doesn't make any sense
matlab -nojvm -r 'readtxt_abc;pause(1);myfunc1(a,b,c);clear' & # there's no need to pipe this to sh
((i++))
done
但是您可以让Bash维护您的计数器变量:
for ((i = 0; i <= p; i++)); do
. . .
done
也许第二行应该是:
echo "$i" > "$i.txt"
但我不知道你为什么要这么做。
关于bash - while循环BASH中的语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10781290/