I am having some extreme problems with a script that I am writing. The problem is that I have to get some stats and md5sum results for all files recursively in a directory.
The problem is that the script is somehow being roasted by BASH and md5sum and stat are complaining when I try to work with filenames and directories with spaces or $s in them.
The script looks something like this:
Code:
#!/bin/bash
#set -x
find /opt -fprint /var/tmp/findresult.txt
# Right, now we delimit all spaces in the file names:
sed -i -e 's/ /\\ /g' /var/tmp/findresult.txt
# and we delimit all dollars in the file names:
sed -i -e 's/\$/\\\$/g' /var/tmp/findresult.txt
for ObjectPath in `cat /var/tmp/findresult.txt`
do
# If it's a file, then get the MD5Sum of the file, otherwise mark it s a directory.
if [ -d $ObjectPath ]
then
CheckSum=""
else
CheckSum=`md5sum "$ObjectPath" | awk -F" " '{print $1}'`
fi
# Get the appropriate File Statistics
FileStats=`stat "$ObjectPath"`
done
A sample of the contents of the /var/tmp/findresult.txt file is:
Code:
/opt/plan/file\$export.xml
/opt/plan/other\ file.xml
I get the following errors:
Code:
md5sum: /opt/plan/file\$export.xml: No such file or directory
stat: cannot stat`/opt/plan/file\\$export.xml': No such file or directory
md5sum: /opt/plan/other\ file.xml: No such file or directory
stat: cannot stat`/opt/plan/other\\ file.xml': No such file or directory
If I run the stat and md5sum commands in the bash shell (copying the file name out of the findresult.txt file) they both execute properly.
My Linux box is 2.6.9-67.ELsmp Red Hat.
Does anyone know what I am doing wrong and how can I fix it?