EpiMan
EpiMan

Reputation: 839

Generating sequences of numbers and characters with bash

I have written a script that accept two files as input. I want to run all in parallel at the same time on different CPUs.

inputs:

x00.A   x00.B
x01.A   x01.B
...
x30.A   x30.B

instead of running 30 times:

./script x00.A x00.B
./script x01.A x01.B
...
./script x30.A x30.B

I wanted to use paste and seq to generate and send them to the script.

paste & seq | xargs -n1 -P 30 ./script

But I do not know how to combine letters and numbers using paste and seq commands.

Upvotes: 0

Views: 541

Answers (2)

mirabilos
mirabilos

Reputation: 5327

for num in $(seq -f %02.f 0 30); do
    ./script x$num.A x$num.B &
done
wait

Although I personally prefer to not use GNU seq or BSD jot but (ksh/bash) builtins:

num=-1; while (( ++num <= 30 )); do
    ./script x$num.A x$num.B &
done
wait

The final wait is just needed to make sure they all finish, after having run spread across your available CPU cores in the background. So, if you need the output of ./script, you must keep the wait.

Putting them into the background with & is the simplest way for parallelism. If you really want to exercise any sort of control over lots of backgrounded jobs like that, you will need some sort of framework like GNU Parallel instead.

Upvotes: 2

rottweilers_anonymous
rottweilers_anonymous

Reputation: 406

You can use pure bash for generating the sequence:

printf "%s %s\n" x{00..30}.{A..B} | xargs -n1 -P 30 ./script

Happy holidays!

Upvotes: 0

Related Questions