20

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.

6 Answers6

27

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.

Radu Rădeanu
  • 169,590
11

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
5

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
Jacob Vlijm
  • 83,767
5

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

Ole Tange
  • 1,710
  • 1
    I love parallel and try to suggest its usage wherever I can. One question though to your specific option: why did you prefer to suggest this form instead of parallel -N0 doit ::: {1..10} – Phillip -Zyan K Lee- Stockmann Feb 16 '17 at 13:07
  • 1
    The {1..10} does not work in (t)csh/fish/ash, and the {1..10} construct is limited by the length of the command line, so this will not work: {1..1000000000} whereas seq 1000000000 will work just fine. But for most situations {1..10} will work as expected, and I often use that, too. – Ole Tange Feb 16 '17 at 15:49
4

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
0

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 .

nux
  • 38,017
  • 35
  • 118
  • 131
  • 4
    I think the question is specific on running the command a defined number of times, not so much at a defined interval. – Jacob Vlijm Sep 12 '14 at 19:36