简体   繁体   中英

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. Each file is of the name form List.1.rds, ... List.500.rds . The way I can see which ones are missing is by the following code in 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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