Count the number of times my script run

Hello world. I have a computer with last ubuntu MATE. I run a script and to count the times that rus. I don't understand too much oif this.
The script is:

#!/bin/sh
while :
do
firefox https://www.myweb.com/ &
sleep 3
pkill -f firefox &
for i in {1. .5}
do
echo "Hai $i"
done

but not works. I only need to know the number of times that run.
Please, help. and a lot of thanks

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:

  1. An infinite while loop surrounds the rest of the commands in the script. This means that the while loop will never end.

  2. 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.

  3. 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).

  4. 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.)

  5. 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
    
  6. The commands in steps 2 through 5 are repeated ad infinitum, or, in technical parlance, 'til the cows come home. :wink:

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
2 Likes

MANY MANY MANY THANKS¡¡¡¡
Works perfectly.
I only change:
echo "This loop has been run $NUMBER_OF_LOOPS times."
for
notify-send "This loop has been run $NUMBER_OF_LOOPS times."
:slight_smile: you make my day.
Asis

Hi Gordon, please, could you give me another hand on it?
In this script (works perfectly, thanks) now I need to open 3 webs in aleatory mode. Please, could you help me?
Thanks a lot.
Asis

All right, so do you want to open only one tab at a time, but that tab randomly chosen from a list of three possible web pages -- or do you want to open all three web pages at the same time, but in a random order?

First, I'll assume that you want the former. Let me rewrite the script I quoted above:

#!/bin/bash

declare -a WEB_PAGES
WEB_PAGES[0]="https://someplace.com/index.html"
WEB_PAGES[1]="https://anotherplace.com/page.php"
WEB_PAGES[2]="https://xyz.abc/"

for ((NUMBER_OF_LOOPS=1; ; NUMBER_OF_LOOPS++)); do
  firefox "${WEB_PAGES[RANDOM % 3]}" &
  sleep 3
  pkill -f firefox

  notify-send "This loop has been run $NUMBER_OF_LOOPS times."
done

Now, we create an array variable at the top of the program which contains the URL of each of the three Web pages. Each time we run the firefox command in the for loop, we pass Firefox a different URL -- specifically, whatever URL is at a randomly-chosen "index" of the WEB_PAGES array. RANDOM is a special Bash variable which contains a different random number between 0 and 32767 each time you read from it; the % 3 divides the random number by 3 and then finds the remainder, using that as the array index. (Dividing a random number and then finding the remainder is a classic mechanism for obtaining random numbers between zero and a set limit -- in this case, between zero and two.)

As you can see, we index array elements 0 through 2. Why not index the array elements 1 through 3? Because this is a computer we're talking about, and computer people decided a long time ago that starting array indexes at 0 was a good idea. So we're stuck with it. Well, it works OK for us anyway.

If you want three tabs opened simultaneously with different Web pages in each tab each time (I find that unlikely, though, based on my possibly-wrong conception of your situation), you can use code like this:

#!/bin/bash

WEB_PAGES="https://someplace.com/index.html
https://anotherplace.com/page.php
https://xyz.abc/"

for ((NUMBER_OF_LOOPS=1; ; NUMBER_OF_LOOPS++)); do
  SHUFFLED_WEB_PAGES=$(shuf <<<"$WEB_PAGES")
  firefox $SHUFFLED_WEB_PAGES &
  sleep 3
  pkill -f firefox

  notify-send "This loop has been run $NUMBER_OF_LOOPS times."
done

I hope that was what you wanted.

wow¡¡¡ thanks very much. Works... open only one web any time in aleatory mode. thanks a lot.
Asis

I was going to reply to your (now-deleted) post last night where you needed further assistance, but I had no idea what you actually wanted to do. You didn't exactly word it clearly. Plus, your script opened one page in Chromium and the rest in Firefox. I had no idea what you wanted to do.

I'm assuming that you don't need the extra "open one page in one browser and cycle through the rest of the pages randomly using the other browser" anymore. But if you do, then call me again. And preferably, try to word your requests a little bit better; I know it must be hard to write English if your native tongue is (Spanish? Portuguese? sorry, I'm ignorant), but it's also hard for me to help you effectively if we can't speak the same language, literally and figuratively.

P.S: I'm guessing when you say "aleatory mode", you mean "load stuff randomly", and there's not a technical term "aleatory mode". I Google searched for "aleatory mode" and didn't get any technical results.

Hi Gordon and thanks very much. I deleted because I think I am a bit "heavy" to bother you so many times, sorry.
I am using your two scripts, The "count times" and the "random" (in Spanish "aleatory") in two different scripts and works perfectly (after take a hard look and find the firefox-chromium mistake) so, I think is not necessary to be your headache continually.
Thanks a lot for your help, and sure you win a couple of beers and "tortilla de patata" if I catch you for Spain.
Asis

1 Like

Happy new year @gordon, after a time using this fantastic code, that works perfectly, please, it is possible to open two webs instead one, every time the loop goes?
I mean, open https://someplace.com/index.html and after 5 seconds open https://anotherplace.com/page.php > sleep 30 > begin again the loop.

Thanks a lot.
Asis

Again, I'm going to have to guess what you meant. I'm guessing you want the script to open Firefox to https://someplace.com/index.html, then wait 5 seconds, then open another Firefox tab to https://anotherplace.com/page.php, then pause another 30 seconds, then close Firefox down and repeat the loop.

If that is what you want, here's some code to do such:

#!/bin/bash

for ((NUMBER_OF_LOOPS=1; ; NUMBER_OF_LOOPS++)); do
  firefox "https://someplace.com/index.html" &
  sleep 5
  firefox "https://anotherplace.com/page.php" &
  sleep 30
  pkill -f firefox

  notify-send "This loop has been run $NUMBER_OF_LOOPS times."
done

If you want to open each of the two pages in their own window, add the --new-window option between the URL and the firefox command. Example:

#!/bin/bash

for ((NUMBER_OF_LOOPS=1; ; NUMBER_OF_LOOPS++)); do
  firefox --new-window "https://someplace.com/index.html" &
  sleep 5
  firefox --new-window "https://anotherplace.com/page.php" &
  sleep 30
  pkill -f firefox

  notify-send "This loop has been run $NUMBER_OF_LOOPS times."
done

Of course, you can use the --new-window option in any of the previous versions of the script, if that was what you were really looking for.

Thanks very much @gordon, but I am a bit dummy to explain it.

I mean, open aleatory one web + open aleatory another web, and then, pause 30 seconds, and then, close Firefox down and repeat the loop.

I hope this time explain to you better...
Thanks very much for the help.
Asis

I came across this thread while reading new topics and just want to congratulate gordon on his willingness to help and the clear examples and explanations he gives. Thank you, gordon! It would be great if more people could be as helpful and clear.

I agree, @gordon is a crack¡¡¡ thanks a lot.

So, if that's what you want, then here's some code to pick two random URLs out of a list each time the loop cycles, open one of the URLs, pause 5 seconds, then open the other, wait 30 more seconds, then close down Firefox and repeat:

#!/bin/bash

WEB_PAGES="https://someplace.com/index.html
https://anotherplace.com/page.php
https://xyz.abc/"

for ((NUMBER_OF_LOOPS=1; ; NUMBER_OF_LOOPS++)); do
  SHUFFLED_WEB_PAGES=$(shuf <<<"$WEB_PAGES" | head -2)
  FIRST_WEB_PAGE=$(head -1 <<<"$SHUFFLED_WEB_PAGES")
  SECOND_WEB_PAGE=$(tail -1 <<<"$SHUFFLED_WEB_PAGES")

  firefox $FIRST_WEB_PAGE &
  sleep 5
  firefox $SECOND_WEB_PAGE &
  sleep 30
  pkill -f firefox

  notify-send "This loop has been run $NUMBER_OF_LOOPS times."
done

super¡¡¡¡ thanks very much. Works perfectly :_)
Have a nice and safe weekend.

1 Like