BASH:如何制作一个每 X 秒更新一次并永远重复的脚本?



>假设我有这行代码

#!/bin/bash
xidel -se '//span[@class="last original"][1]' 'https://www.cnbc.com/quotes/?symbol=XAU='
exit 0

输出应在 1,7K+ 左右

但是,此脚本仅运行一次。我要loop forever every 5 or 10 secs,我该怎么办?

你可以做一个循环和睡眠

while :; do <your_command>; sleep <seconds>; done

另一种选择是watch你的命令

watch -n <seconds> <your command> # use "<command>" if your command contains whitespaces

watch命令有一些开箱即用的非常好的功能

–errexit : freeze screen when the command failed
-beep: give a beep when the command failed
-differences: to highlight the differences in output.
#!/bin/bash
for (( ; ; ))
do
clear #put clear if we don't want the script is executed under the old process
xidel -se '//span[@class="last original"][1]' 'https://www.cnbc.com/quotes/?symbol=XAU='
sleep 5 #Every 5 seconds
done

相关内容

最新更新