Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Parallel for-loop in shell script for all major OS

I am using a shell script to compile multiple .tex files automatically. But compiling them sequentially takes very long, especially because I need to compile them multiple times for correct output. Here is a short snippet:

#!/bin/bash
for file in *.tex
do
    pdflatex ...
done

I tried a solution with xargs but I was told, that this does not run on macOS. Sadly I did not find a solution that works on all major OS (Windows/ms-dos, macOS, Linux/unix). Can you give me a solution? I only use Linux and can’t test other systems, but it has to work on them.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

My immediate though was: Why not use make? Then you can run all jobs on 8 cores like this:

make -j8

On the other hand, you can just start each process in the background. You won’t have any control about the number of cores to use, though, unless you put in a lot more effort:

#!/bin/bash
for file in *.tex
do
  pdflatex ... "$file" &
done
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading