如何解决bash脚本中的错误以将负载放在cpu上?

时间:2014-01-10 10:57:16

标签: linux bash

我有两个脚本可以在CPU上加一个峰值。

infinite_loop.bash:

#!/bin/bash
while [ 1 ] ; do
    # Force some computation even if it is useless to actually work the CPU
    echo $((13**99)) 1>/dev/null 2>&1
done

cpu_spike.bash:

#!/bin/bash
# Either use environment variables for NUM_CPU and DURATION, or define them here
for i in `seq ${NUM_CPU}` : do
    # Put an infinite loop on each CPU
    infinite_loop.bash &
done

# Wait DURATION seconds then stop the loops and quit
sleep ${DURATION}
killall infinite_loop.bash

该脚本早先工作正常。现在BUt脚本不能正常工作。它给了我一个错误:

./cpu_spike.bash: line 5: syntax error near unexpected token `infinite_loop.bash'
./cpu_spike.bash: line 5: `    infinite_loop.bash &'

1 个答案:

答案 0 :(得分:0)

错误如下:

for i in `seq ${NUM_CPU}` : do

您需要使用for终止;,而不是:。说:

for i in `seq ${NUM_CPU}` ; do

:是一个空命令。


此外,说:

while [ 1 ] ; do command ; done

模拟无限循环不正确,而实际上确实生成了一个。你可能会注意到:

while [ 0 ] ; do command ; done

也会导致无限循环。正确的方法是:

while true ; do command; done

while : ; do command; done

while ((1)); do command; done

有关产生无限循环的其他变体,请参阅this answer。