I have a (for me atleast) quite complicated question.
#! /bin/bash notify=false a=0 while ((a <= 50)) do echo $a a=$(echo "$a+1" | bc) sleep 3s done
When I run the script, the while loop goes and does it's thing. When it's done, the script exits.
If I set the variable
notify inside the script to true, something needs to happen. And that is that it sets a variable (e.g.
notify_now) to true when $a reaches 25. When
notify_now is set to true, it echo's a notification that doesn't interrupt the while loop. See below:
... 24 25 The variable $a has reached 25 26 ...
But when I set
notify to false, it doesn't give any notifications. See below:
... 24 25 26 ...
Doing the following isn't an option in my actual situation:
#! /bin/bash notify=false a=0 while ((a <= 50)) do echo $a if ((a = 25)) && [[ $notify = true ]] then echo "The variable \$a has reached 25" fi a=$(echo "$a+1" | bc) sleep 3s done
I need a piece of code that is able to be run outside of the loop when a variable is set to true. The code needs to be run in the background so that the while loop can run in the foreground. When that piece of code in the background has noticed that the variable $a in the foreground has reached 25, it echo's a sentence in the terminal.
How far I got:
- The piece of code is probably going to be a function that will be called
- The function will be run in the background but echo's in the foreground (using
&, but I'm really bad at that so that's why I'm asking this)
- The code will be in a loop that will run until $a has reached 25 (so it will be checking $a as fast as a while loop goes until it is 25)
But I can't seem to be able to get any actual code to work.