unix_admin_1234
asked on
Need help with BASH script to check existence of file and use an exclude list
I'd like to create a Bash Script that will allow me to check the existence of a file in a directory and touch that file if it doesn't exist. At the same time, I would like an exclusion list that will exclude certain directories from having that file touched. Please see what I have so far. I'm stuck on the excludes file:
#!/bin/bash
BASEDR="/var/lib/test"
TEMPFILE="/tmp/export_tmp"
CONFFILE="/root/conf/exclu des.conf"
for i in `find $BASEDR -name "*stuff" -print`;
do
ls $i/hello > /dev/null 2>&1
rc=$?
[ $rc -ne 0 ] && echo "$i doesn't have the file."
done
#!/bin/bash
BASEDR="/var/lib/test"
TEMPFILE="/tmp/export_tmp"
CONFFILE="/root/conf/exclu
for i in `find $BASEDR -name "*stuff" -print`;
do
ls $i/hello > /dev/null 2>&1
rc=$?
[ $rc -ne 0 ] && echo "$i doesn't have the file."
done
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
madan1278. Your script is close. However, the FILE_TO_CHECK need to include a way to find the files in a specific directory. For example, I would want the script to look under /var/reps/ and look for a directory ending in .test/filename. There are a lot of directories and subdirectories which include this pattern. Please let me know if this doesn't make sense.
Thanks.
Thanks.
ASKER
Also, I want to include the files to excludes, and not the directories.
Thanks.
Thanks.
Your requirements are very unclear.
It appears you are wanting to check for the existence of the file 'hello' in directories and sub-directories under /var/lib/test (excluding certain directories) and creating hello if it doesn't exist.
Is that correct?
If so, then you can do
It appears you are wanting to check for the existence of the file 'hello' in directories and sub-directories under /var/lib/test (excluding certain directories) and creating hello if it doesn't exist.
Is that correct?
If so, then you can do
#!/bin/bash
BASEDIR=/var/lib/test
CONFFILE=/root/conf/excludes.conf
find $BASEDIR -name "hello" | grep -vf $CONFFILE | xargs -I touch {}
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Tintin, that's exactly what I am looking to do. I like the brevity of your script, but I'm unclear what the xargs -I touch {} does. I tried to run the script and keep getting the error:
+ xargs -I touch '/test{}'
xargs: /git-daemon-export-ok{}: No such file or directory
Thanks.
+ xargs -I touch '/test{}'
xargs: /git-daemon-export-ok{}: No such file or directory
Thanks.
Sorry, my typo. It can actually be simplified to
find $BASEDIR -name "hello" | grep -vf $CONFFILE | xargs touch
find $BASEDIR -name "hello" | grep -vf $CONFFILE | xargs touch
ASKER
Both scripts worked.
Thanks.
Thanks.
I don't have a system to test the below code but it should work..
#!/usr/bin/bash
#Input the filename with absolute path as a argument to the script.
FILE_TO_CHECK=$1
#Input the absolute path of dirs you want to exclude in the exclude file referenced below
EXCLUDE_LIST=`cat /tmp/exclude.txt` #Variable just for reference
FILENAME=`basename $1`
DIRNAME=`dirname $1`
grep $DIRNAME /tmp/exclude.txt > /dev/null
if [ $? -eq 0 ]
then
[ ! -f $1 ] && touch $1
else
echo "This dir is excluded"
fi