whereas :
do
echo Maintain working
echo "Press CTRL+C to exit"
sleep 3
finished
You are able to do the identical factor in a single line by separating the strains proven above with semicolons as proven within the instance under.
whereas true; do echo Maintain working; echo 'Press CTRL+C'; sleep 3; finished
The command under would work the identical. I want whereas true as a result of it appears apparent, however any of the examples proven will work as does this one:
whereas [ 1 ]
do
echo Maintain working
echo "Press CTRL+C to exit"
sleep 3
finished
Utilizing for
The for command additionally gives a simple method to loop without end. Whereas not fairly as apparent as the whereas true choice, the syntax within reason simple. You possibly can simply substitute the parameters in a traditional for loop (the beginning, situation and increment values) that will usually look one thing like this:
$ for (int i = 0; i < 5; i++)
As a substitute, use the for command with no such values as within the instance under.
$ for (( ; ; ))
> do
> echo Maintain your spirits up
> echo “Press CTRL+C to exit”
> sleep 3
> finished
Maintain your spirits up
“Press CTRL+C to exit“
Maintain your spirits up
“Press CTRL+C to exit“
Maintain your spirits up
“Press CTRL+C to exit“
And, clearly, press CTRL-C if you need to exit the loop.
Why loop without end?
In fact, you’re not ever going to need to loop without end, however having loops run till you cease them can usually be very helpful. The script under runs till 5:00 p.m. It makes use of the date command with the +%H choice to verify the time, then reminds you that it’s time to go residence and exits. The sleep 60 command was included to restrict the time checking to as soon as a minute. In any other case, this script may gobble up lots of CPU time.