Ubuntu: Run a program X times



Question:

How do I execute a program X times in the terminal?

I read that I have to do a bin/bash txt, but I don't know how to execute this X times.


Solution:1

You can use xargs and seq. Generally:

seq nr_of_times_to_repeat | xargs -Iz command

For example:

seq 10 | xargs -Iz xdotool click 1  

will execute the xdotool click 1 command for 10 times.


Solution:2

Open a Terminal and use the following bash command:

for i in {1..5}; do xdotool click 1; done  

With a bit of verbosity and 1s delay:

for i in {1..5}; do echo click \#$i && xdotool click 1 && sleep 1; done  click #1  click #2  click #3  click #4  click #5  


Solution:3

This should do:

#!/bin/bash    x=1  while [ $x -le 10 ]  do    <command to run>    x=$(( $x + 1 ))  done  

where 10 is the number of times to run the command

if you need to build in a little break:

#!/bin/bash    x=1  while [ $x -le 10 ]  do    <command to run>    sleep 1    x=$(( $x + 1 ))  done  

Copy the script into an empty file, replace <command to run> by your xdotool command, save it as run_xdotool.sh, run it by the command:

sh /path/to/run_xdotool.sh  

Alternatively, you can make it executable and simply run it by

/path/to/run_xdotool.sh  

Another solution: using xdotool's built in repeat option

Since you mention to use it to do clicks, the easiest might be to use xdotool's own built-in repeat option. The format is:

xdotool click --delay <delay> --repeat <repeats> <button>  (delay in milliseconds between the clicks)  

To do 10 mouse clicks (button 1) at a row, one second in between, the command is:

xdotool click --delay 1000 --repeat 10 1  


Solution:4

If you have GNU Parallel you can run:

seq 10 | parallel -N0 doit  

All new computers have multiple cores, but most programs are serial in nature and will therefore not use the multiple cores. However, many tasks are extremely parallelizeable:

  • Run the same program on many files
  • Run the same program for every line in a file
  • Run the same program for every block in a file

GNU Parallel is a general parallelizer and makes is easy to run jobs in parallel on the same machine or on multiple machines you have ssh access to.

If you have 32 different jobs you want to run on 4 CPUs, a straight forward way to parallelize is to run 8 jobs on each CPU:

Simple scheduling

GNU Parallel instead spawns a new process when one finishes - keeping the CPUs active and thus saving time:

GNU Parallel scheduling

Installation

A personal installation does not require root access. It can be done in 10 seconds by doing this:

(wget -O - pi.dk/3 || curl pi.dk/3/ || fetch -o - http://pi.dk/3) | bash  

For other installation options see http://git.savannah.gnu.org/cgit/parallel.git/tree/README

Learn more

See more examples: http://www.gnu.org/software/parallel/man.html

Watch the intro videos: https://www.youtube.com/playlist?list=PL284C9FF2488BC6D1

Walk through the tutorial: http://www.gnu.org/software/parallel/parallel_tutorial.html

Sign up for the email list to get support: https://lists.gnu.org/mailman/listinfo/parallel


Solution:5

You can use a C style for loop which has the advantage over the brace-expansion version ({1..5}) of being able to use variables to specify the end points. Either version is better than using an external utility (seq).

t=5  for ((x = 1; x <= t; x++))  do      xdotool click 1  done  

All on one line:

t=5; for ((x = 1; x <= t; x++)); do xdotool click 1; done  

Or you might be able to do it without a loop (for this specific utility and function):

xdotool click --repeat 5 --delay 50 1  


Solution:6

Simply you can use watch command :

watch -n x <your command>  

change x to time in seconds .

Example :

watch -n 1 xdotool click 1   

So xdotool click 1 will be repeated every 1 s , terminate it when your done .


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »