mardi 8 août 2017

bash if statement checking path matches list of paths

I have a script that I need to check for the existence of directories on an external storage, and it should exit if any do not match - except for those listed in the file list

they are listed with their full paths and sometimes contain special characters, spaces, [],{} and not utf characters. there is one path per line.

$ cat /data/list

/data/test [test] {test}
/data/test directory
/data/test name

i have the script working without the exclusion list. the checking against an exclusion list does not work. i.e. it does not break when /data/test [test] {test} is found on the list, and instead, exits because it doesn't exist at /external/data/.

i think the problem is matching due to the formatting of $(< /data/list) without line breaks, but in my experiments with and without "" and =~ etc. have been fruitless.

#!/bin/bash
for i in /data/storage/*; do
        if [[ ! -h "$i" ]]; then
                if [[ "$i" == "$(< /data/list)" ]]; then 
                break; else
                        if [ -e "/external//data/${i##*/}" ]; then
                        echo "/external/data/${i##*/} exists!"; else
                        echo "$i doesn't exist at /external/data/${i##*/}";
                        exit 1
                        fi
                fi
        fi
done

Aucun commentaire:

Enregistrer un commentaire