gpt4 book ai didi

linux - 带有 while 循环的 Bash 脚本,直到满足动态条件

转载 作者:太空宇宙 更新时间:2023-11-04 12:11:34 26 4
gpt4 key购买 nike

#!/bin/bash
/home/user/script.sh > output.txt
VAR0=$(cat output.txt | grep -A 3 "word")
A0=$(echo $VAR0 | awk 'BEGIN { FS = "[:X]" } {printf $3}')
while [ $A0 \< 2.37 ]
do echo $A0
sleep 10
done
/home/user/script2.sh

我想做的是当 output.txt 中 script.sh 的输出 > 2.37 时运行 script2.sh

问题是,script.sh 的输出是动态的,所以每次 while 循环都必须从 output.txt 获取新的输出,直到满足条件并且可以运行 script2.sh。

如何在每次 while 循环时刷新 $A0 的值?

我现在得到的是 $A0 永久女巫的第一个值小于 2.37

需要一些时间,$A0 中的 script.sh 的输出变为 > 2.37

我已经考虑过

/home/user/script.sh
sleep 180
/home/user/script2.sh

但是 3 分钟太多了,output.txt 的变化可能发生在 2 到 3 分钟之间的任何地方

基本上我想要的是在满足 script.sh 中的条件时运行 scrip2.sh...

有什么想法吗?

谢谢!

最佳答案

A0的计算放在循环中,当A0 >= 2.37时退出循环:

#!/bin/bash

while true ; do
/home/user/script.sh > output.txt
VAR0=$(cat output.txt | grep -A 3 "word")
A0=$(echo $VAR0 | awk 'BEGIN { FS = "[:X]" } {printf $3}')
echo $A0
if (( $(echo "$A0 >= 2.37" | bc -l) )) ; then
break
fi
sleep 10
done

/home/user/script2.sh

请注意,bash 本身无法比较 float 。你需要像这样的 hack

if (( $(echo "$A0 >= 2.37" | bc -l) )) ; then

参见 How to compare two floating point numbers in Bash?

关于linux - 带有 while 循环的 Bash 脚本,直到满足动态条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48723371/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com