lundi 29 décembre 2014

BASH: IFfing throuth two (or more) folders to find files matching pattern

I have a bash-related question. To start the explaination. I had a folder structure looking like this:



config--
|-config-chain.conf
|-somefile1.txt
|-somefile2.txt
|-somefile3.txt
|-somefile4.txt
|-somefile5.txt
|-somefile6.txt


config-chain.conf contains text looking like this:



somefile1
somefile2
somefile3
somefile4
somefile5
somefile6


Before today, all those txt files were in one folder, so iterating through this was simple.


But the specification has changed and I have to do this the new way.



config--
|-config-chain.conf
|
|--folder1--
|-somefile1.txt
|-somefile2.txt
|-somefile3.txt
|--folder2--
|-somefile4.txt
|-somefile5.txt
|-somefile6.txt


Before that, I was iterating through this with a simple loop. It looked like this:



while read config-chain
do
if [ -f $config-chain.txt ];
then
echo "Config file found"
else
echo "Config file not found"
fi
done < config-chain.conf


But now the files are in two different folders. My actual approach is looking like this:



while read config-chain
do
if [ -f folder1/$config-chain.txt ] || [ -f folder2/$config-chain.txt ];
then
echo "Config file found"
else
echo "Config file not found"
fi
done < config-chain.conf


It is looking ugly for me, cause I'm looking for the existence of a file in both folders. I don't know how this will look in the future, maybe there will be 15 folders, so imagine this OR with 15 statements... Is there a way to do this cleaner? Maybe with find? Or a more clean way to do this with IF?


Aucun commentaire:

Enregistrer un commentaire