简体   繁体   English

Bash脚本:目前,我在一个目录中应该有500个文件,如果缺少任何文件,如何停止bash脚本?

[英]Bash Scripting: I currently am supposed to have 500 files inside a directory, how can I stop a bash script if any files are missing?

I currently have a directory that is supposed to have 500 files. 我目前有一个目录,应该具有500个文件。 Each file is of the name form List.1.rds, ... List.500.rds . 每个文件的名称格式为List.1.rds, ... List.500.rds The way I can see which ones are missing is by the following code in bash: 通过bash中的以下代码,可以查看丢失的内容:

for((i=1; i<=500; i++)); do name="List.${i}.rds"; [[ ! -e "$name" ]] && echo "missing $name"; done

If a file is missing, it returns the missing file name. 如果缺少文件,它将返回丢失的文件名。 However, I would like to go one step further and stop the entire script should any file be missing. 但是,如果缺少任何文件,我想更进一步,并停止整个脚本。 Is there a way to do this? 有没有办法做到这一点? thanks. 谢谢。

It can be as simple as setting a flag when a file is missing: 它可以像缺少文件时设置标志一样简单:

miss=0
for ((i=1;i<=500;i++)); do
    file=List.$i.rds
    if [[ ! -e $file ]]; then
        echo "Missing $file"
        miss=1
    fi
done

# exit if "miss" flag is 1
((miss)) && exit 1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM