检查shell中是否有几个globs匹配

时间:2015-04-21 17:39:58

标签: shell if-statement

我很难理解shell中一行的if语法:

if [ ! -f *file1.txt* -a ! -f *file2.txt* -a ! -f *file3.txt* ]; then
    sbatch file.sh
fi

使用*是因为我的文件备份为#file.txt.1#format。

据我所知,!创建一个'if not',如果字符串是文件'-f',但是我没有找到-a标志的任何函数。

我只想在所有这些文件都不存在的情况下提交file.sh。

有人可以提供帮助吗?

1 个答案:

答案 0 :(得分:2)

一个简单的实现,与任何POSIX shell兼容:

exists_any() {
  while [ "$#" -gt 0 ]; do   # as long as we have command-line arguments...
    [ -e "$1" ] && return 0  # if first argument names a file that exists, success
    shift                    # remove first argument from the list
  done
  return 1                   # nothing matched; report failure
}

if ! exists_any *file1.txt* *file2.txt* *file3.txt*; then
  sbatch file.txt
fi