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.
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.
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.
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
$
. It's just there to indicate a shell prompt like sylvain@sylvain-ThinkPad-T430s:~$
. We usually add it to command line answers to differentiate the command from its results.
– Sylvain Pineau
Sep 12 '14 at 16:32
#!/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
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
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:
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:
GNU Parallel instead spawns a new process when one finishes - keeping the CPUs active and thus saving time:
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
parallel -N0 doit ::: {1..10}
– Phillip -Zyan K Lee- Stockmann
Feb 16 '17 at 13:07
{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
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
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 .