运行多个批处理文件,但有例外

时间:2019-01-21 17:27:36

标签: bash

我大约有100 files.sh可以通过以下方式运行:

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

但是在这100个文件中,有5个我不想被运行,所以我想说些类似的话:

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

如果您有想法,谢谢您的帮助吗?

4 个答案:

答案 0 :(得分:3)

您可以像下面这样使用扩展的glob(请查看Pattern Matching section in the reference manual的结尾):

shopt -s extglob

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

另一种可能性是:

# 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

如果要跳过的文件很多,在数组或关联数组中声明它们可能会更容易...但是由于您只有5个,所以我给您的两种可能性都应该没事。

答案 1 :(得分:2)

只需使用if来保护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

答案 2 :(得分:1)

或带有conf文件:

for files in The_job_*;do
grep $files $FILE_BASH_TO_EXCLUDE >/dev/null 2>&1
    if [ $? -ne 0 ]
    then
        msgOut "Do not run this one"
    else
        sbatch $files;
    fi
done

使用此解决方案,如果您添加例外,则无需编辑代码;)

答案 3 :(得分:1)

使用case语句:

for file in The_job_*; do
  case $file in
  The_job_345 | The_job_567 | The_job_789)
    ;;
  *)
    sbatch "$file" ;;
  esac
done