LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Shell scripting problems for md5sum & stat for filenames with spaces and $s (http://www.linuxquestions.org/questions/programming-9/shell-scripting-problems-for-md5sum-and-stat-for-filenames-with-spaces-and-%24s-712530/)

QuietLeni 03-18-2009 11:02 AM

Shell scripting problems for md5sum & stat for filenames with spaces and $s
 
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?

Hko 03-18-2009 11:45 AM

Keeping it simple:
Code:

#!/bin/bash

find /tmp/q  | while read ObjectPath; do
    if [ -d "$ObjectPath" ]; then # double quote variables containing file name.
        CheckSum=""
    else
        CheckSum=`md5sum "$ObjectPath" | awk -F" " '{print $1}'`
    fi
    FileStats=`stat "$ObjectPath"`
done


Kenhelm 03-18-2009 06:38 PM

In the original script you need to 'eval' $ObjectPath to get a double expansion.
e.g.
Unexpanded: $ObjectPath
First expansion: /opt/plan/file\$export.xml
Second expansion: /opt/plan/file$export.xml
Try
Code:

CheckSum=`eval md5sum "$ObjectPath" | awk -F" " '{print $1}'`

FileStats=`eval stat "$ObjectPath"`


QuietLeni 03-19-2009 05:38 AM

Got it!!!!
 
Thanks for the replies, but I found the answer. It was even simpler:

Code:

#!/bin/bash
#set -x

find /opt -fprint /var/tmp/findresult.txt

while read ObjectPath
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 < /var/tmp/findresult.txt

If I just redirect into the file and then use the "while read" construct, the shell assumes that each line is a path, not just a space.

Again, thanks!


QuietLeni

:)


All times are GMT -5. The time now is 07:33 AM.