Bash exec处于无限循环中并不起作用

时间:2014-10-22 10:22:12

标签: bash while-loop exec infinite-loop

#!/bin/bash 
while :
do
    echo twerkin
    exec free -h > /ver/www/raspberry/load.txt
    exec /opt/vc/bin/vcgencmd measure_temp > /var/www/raspberry/heat.txt
done

这就是我所做的,我将在我的网站上阅读,但那不是问题。问题是它给了我这个错误:

pi@raspberrypi ~ $ sh showinfo.sh
showinfo.sh: 7: showinfo.sh: Syntax error: "done" unexpected (expecting "do")

我用Raspbian(Debian Wheezy)在我的树莓派上运行这个

2 个答案:

答案 0 :(得分:2)

您似乎无法理解exec关键字的作用。 使用while命令替换当前脚本(基本上是free循环)。

要运行命令,只需指定它。

#!/bin/bash

while : ; do
    free -h
    /opt/vc/bin/vcgencmd measure_temp 
done

(我省略了重定向,因为你在每次迭代时都覆盖了旧的结果。你真的想要什么?)

答案 1 :(得分:0)

尝试更改脚本中的一些内容

#!/bin/bash 

while : ;
do
    echo twerkin

    #### commands to execute are not need to be prefixed with exec
    #### + concatenate output with >>
    free -h >> /ver/www/raspberry/load.txt

    #### same here
    /opt/vc/bin/vcgencmd measure_temp >> /var/www/raspberry/heat.txt

    #### you probably would want to add a delay here, because in other way
    #### the script will be executed probably way too fast
    # sleep 0.01

done