Skip to main content
added 188 characters in body
Source Link
FelixJN
  • 14.1k
  • 2
  • 36
  • 56

Is there a possibility to watch a command until the output remains the same?

With watch -g, one can check if a commands results changed and use this as a trigger to go to the next step in a script. But what if I want to check for the moment a command returns the same output for two consecutive (or a defined number of consecutive) runs?

Something like

 watch --exit-on-constant-output --number-of-minimum-identical-runs=10 <command> 

Currently the only way that comes into my mind would be a script that saves the output in the first run and then compares like:

#!/bin/bash breaklimit=4 n=0 old=$(command) while ((1)) ; do current=$(command) if [[ "$old" == "$current" ]] ; then echo "command gave same output" n=$((n+1)) echo "number of identical, consecutive runs: $n" if [[ $n -eq $breaklimit ]] ; then   break fi else echo "command gave different output" old="$current" n=0 sleep 2 fi done echo "run next command" 

Works fine for e.g. using $(($RANDOM/100010000)) as command. But maybe there is a more elegant way?

PS: Maybe some made-up use case will help. Let's say I connected a thermometer to my processing machine and want to start production once the temperature has leveled. So I will query the thermometer every 60 seconds for the current temperature.

Is there a possibility to watch a command until the output remains the same?

With watch -g, one can check if a commands results changed and use this as a trigger to go to the next step in a script. But what if I want to check for the moment a command returns the same output for two consecutive (or a defined number of consecutive) runs?

Something like

 watch --exit-on-constant-output --number-of-minimum-identical-runs=10 <command> 

Currently the only way that comes into my mind would be a script that saves the output in the first run and then compares like:

#!/bin/bash old=$(command) while ((1)) ; do current=$(command) if [[ "$old" == "$current" ]] ; then echo "command gave same output" break else echo "command gave different output" old="$current" sleep 2 fi done echo "run next command" 

Works fine for e.g. using $(($RANDOM/1000)) as command. But maybe there is a more elegant way?

PS: Maybe some made-up use case will help. Let's say I connected a thermometer to my processing machine and want to start production once the temperature has leveled. So I will query the thermometer every 60 seconds for the current temperature.

Is there a possibility to watch a command until the output remains the same?

With watch -g, one can check if a commands results changed and use this as a trigger to go to the next step in a script. But what if I want to check for the moment a command returns the same output for two consecutive (or a defined number of consecutive) runs?

Something like

 watch --exit-on-constant-output --number-of-minimum-identical-runs=10 <command> 

Currently the only way that comes into my mind would be a script that saves the output in the first run and then compares like:

#!/bin/bash breaklimit=4 n=0 old=$(command) while ((1)) ; do current=$(command) if [[ "$old" == "$current" ]] ; then echo "command gave same output" n=$((n+1)) echo "number of identical, consecutive runs: $n" if [[ $n -eq $breaklimit ]] ; then   break fi else echo "command gave different output" old="$current" n=0 sleep 2 fi done echo "run next command" 

Works fine for e.g. using $(($RANDOM/10000)) as command. But maybe there is a more elegant way?

PS: Maybe some made-up use case will help. Let's say I connected a thermometer to my processing machine and want to start production once the temperature has leveled. So I will query the thermometer every 60 seconds for the current temperature.

Source Link
FelixJN
  • 14.1k
  • 2
  • 36
  • 56

Observe command until output remains the same. Inverse of `watch -g`

Is there a possibility to watch a command until the output remains the same?

With watch -g, one can check if a commands results changed and use this as a trigger to go to the next step in a script. But what if I want to check for the moment a command returns the same output for two consecutive (or a defined number of consecutive) runs?

Something like

 watch --exit-on-constant-output --number-of-minimum-identical-runs=10 <command> 

Currently the only way that comes into my mind would be a script that saves the output in the first run and then compares like:

#!/bin/bash old=$(command) while ((1)) ; do current=$(command) if [[ "$old" == "$current" ]] ; then echo "command gave same output" break else echo "command gave different output" old="$current" sleep 2 fi done echo "run next command" 

Works fine for e.g. using $(($RANDOM/1000)) as command. But maybe there is a more elegant way?

PS: Maybe some made-up use case will help. Let's say I connected a thermometer to my processing machine and want to start production once the temperature has leveled. So I will query the thermometer every 60 seconds for the current temperature.