Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run several batch files but with exceptions

Tags:

bash

I have around 100 files.sh to run by using:

for files in The_job_*;do
sbatch $files;
done

but in one of these 100 files I have 5 that I do not want to be ran, so I would like to say something like:

for files in The_job_*;do
sbatch $files except for "The_job_345", "The_job_567", "The_job_789";
done

Thanks you for your help if you have an idea?

like image 584
bewolf Avatar asked Sep 17 '25 03:09

bewolf


2 Answers

You could use extended glob (have a look at the end of the Pattern Matching section in the reference manual) like so:

shopt -s extglob

for file in The_job_!(345|567|789); do
    # Don't forget the quotes:
    sbatch "$file"
done

Another possibility is:

# If using bash 3, you'll need to turn extglob on (uncomment the following line)
# shopt -s extglob

for file in The_job_*; do
    if [[ $file = The_job_@(345|567|789) ]]; then
        continue
    fi
    sbatch "$file"
done

If you have lots of files to skip, it's probably easier to declare them in an array or an associative array... but since you only have 5, either of the possibilities I gave you should be alright.

like image 157
gniourf_gniourf Avatar answered Sep 18 '25 17:09

gniourf_gniourf


Just use an if to guard sbatch.

for files in The_job_*; do
  if ! [[ $files = "The_job_345" || $files = "The_job_567" || $files =  "The_job_789" ]]; then
    sbatch $files;
  fi;
done
like image 44
Hoblovski Avatar answered Sep 18 '25 16:09

Hoblovski