LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 03-18-2009, 11:02 AM   #1
QuietLeni
LQ Newbie
 
Registered: Sep 2008
Location: South East England
Distribution: Red Hat
Posts: 11

Rep: Reputation: 0
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?
 
Old 03-18-2009, 11:45 AM   #2
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 108Reputation: 108
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
 
Old 03-18-2009, 06:38 PM   #3
Kenhelm
Member
 
Registered: Mar 2008
Location: N. W. England
Distribution: Mandriva
Posts: 329

Rep: Reputation: 140Reputation: 140
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"`
 
Old 03-19-2009, 05:38 AM   #4
QuietLeni
LQ Newbie
 
Registered: Sep 2008
Location: South East England
Distribution: Red Hat
Posts: 11

Original Poster
Rep: Reputation: 0
Cool 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

 
  


Reply

Tags
bash, md5sum, scripting, shell script, stat


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
list filenames with spaces in a shell script xp_newbie Programming 6 03-15-2009 07:46 PM
Scripting & Arrays : How to fill an array with filenames which consist of spaces? klss Linux - Software 5 01-19-2009 12:16 AM
bash scripting: filenames with spaces joanq Programming 2 05-24-2005 12:50 PM
Scripting question reg. filenames with spaces Awfki Linux - Newbie 3 05-16-2004 08:01 PM
BASH scripting problem, spaces in filenames / using basename textures Programming 24 11-16-2003 01:41 AM


All times are GMT -5. The time now is 01:43 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration