Do you mean that every time you run the script, you want the script to increment some counter somewhere (probably stored in a file), so that you can count how many times the script has been executed? Or, do you instead want to know how many times the while
loop in the script has cycled around and executed all the statements within?
If either one is what you want, then there's no doubt you're doing it wrong. I didn't intend for that to sound offensive; I'm just stating fact.
So let's take this program apart, line by line:
-
An infinite while
loop surrounds the rest of the commands in the script. This means that the while
loop will never end.
-
At the start of a cycle of the loop, Firefox is launched and is opened to the URL https://www.myweb.com/
. Firefox is put into the background so that the rest of the statements of the script can run without waiting for Firefox to exit first.
-
The script pauses for three (3) seconds, but during this three-second period, Firefox loads up the Web page and displays the page on-screen (I assume).
-
When three seconds are up, Firefox is terminated -- presumably to close it down. (Why is the pkill
statement put into the background? It shouldn't take very long at all for the termination to take place.)
-
The program prints the following on the terminal (or into a file, if standard output is redirected somewhere else):
Hai 1
Hai 2
Hai 3
Hai 4
Hai 5
-
The commands in steps 2 through 5 are repeated ad infinitum, or, in technical parlance, 'til the cows come home. 
Now, I'll address what you asked about.
First of all, on Debian-based systems (Ubuntu MATE is Debian-based), /bin/sh
does not point to the Bash shell by default; it points to a more spartan, simpler, less featureful shell called the Debian Almquist Shell (dash
). I can tell you from experience that the dash
does not support the {1. .5}
syntax like you used above, whereas bash
does. So first thing is, change the top line from #!/bin/sh
to #!/bin/bash
.
Second, you have what would be called in other programming languages a bracket mismatch: You have a done
statement at the end of the program which terminates execution of the inner for
loop, but not one to mark the end of the code within the outer while
loop. Add another done
statement to the end of the program, and for goodness's sake, indent the program for your own better understanding:
#!/bin/bash
while :; do
firefox https://www.myweb.com/ &
sleep 3
pkill -f firefox &
for i in {1. .5}; do
echo "Hai $i"
done
done
Third, to count the number of times the loop is run, you can change the while
loop into a for
loop with a syntax like this (whole modified script shown here):
#!/bin/bash
for ((NUMBER_OF_LOOPS=1; ; NUMBER_OF_LOOPS++)); do
firefox https://www.myweb.com/ &
sleep 3
pkill -f firefox &
for i in {1. .5}; do
echo "Hai $i"
done
echo "This loop has been run $NUMBER_OF_LOOPS times."
done
Fourth, is the inner for i in {1..5}
loop actually doing something useful, or was it a failed attempt to count runs of the while
loop? If it doesn't do anything useful, get rid of it:
#!/bin/bash
for ((NUMBER_OF_LOOPS=1; ; NUMBER_OF_LOOPS++)); do
firefox https://www.myweb.com/ &
sleep 3
pkill -f firefox &
echo "This loop has been run $NUMBER_OF_LOOPS times."
done
Maybe it had a use, though. I don't know; you tell me.
Fifth, like I asked above: What's with the backgrounding operator (&
) after the pkill
statement? pkill
should not be backgrounded, really; if Firefox isn't terminated by the time the next Firefox is started, then you'll get a "Firefox is already running" message. Remove that ampersand:
#!/bin/bash
for ((NUMBER_OF_LOOPS=1; ; NUMBER_OF_LOOPS++)); do
firefox https://www.myweb.com/ &
sleep 3
pkill -f firefox
echo "This loop has been run $NUMBER_OF_LOOPS times."
done