Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place! |
| Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
|
04-23-2006, 06:52 AM
|
#1
|
|
Member
Registered: Mar 2004
Distribution: Debian Sarge, FC4
Posts: 100
Rep:
|
Bash script - escaping whitespaces
Hi all,
I wrote a little script I'm using to do checksums on my backups.
Code:
typhon:/var/www# cat /var/www/md5chk.sh
#!/bin/bash
checksumDir="/home/www-data/checksums/"
if [ $# -lt 3 ] ; then
echo "* Error: $0 [directory] [type] [number]"
exit 1
else
if [ -d $1 ] ; then
for i in $( find $1 -type f ); do
md5sum -b $i >> $checksumDir/$2.$3
done
else
echo "* Error: $1 is not a directory"
exit 1
fi
fi
echo "Successfully completed."
exit 0
My problem is with filename with whitespace characters in them. I can't quite work out how to make $i escape it's whitespace chars.
I'd imagine it's gonna something along the lines of: sed 's/ /\\ /' cause I can get it working with `echo "hello world" | sed 's/ /\\ /'`, I'm not sure how to apply this with the $i variable.
Could someone help me out with this? I've been googling for a while with no luck lol.
Cheers.
|
|
|
|
04-23-2006, 07:26 AM
|
#2
|
|
Senior Member
Registered: Oct 2003
Posts: 3,057
Rep:
|
Put the $i in quotes "$i"
|
|
|
|
04-23-2006, 08:41 AM
|
#3
|
|
Member
Registered: Mar 2004
Distribution: Debian Sarge, FC4
Posts: 100
Original Poster
Rep:
|
Quote:
|
Originally Posted by homey
Put the $i in quotes "$i"
|
I have already tried that. Still get errors, I need the whitespaces escaped.
|
|
|
|
04-23-2006, 09:02 AM
|
#4
|
|
Member
Registered: Mar 2004
Distribution: Debian Sarge, FC4
Posts: 100
Original Poster
Rep:
|
OK kinda got a little further here.
Code:
for i in $( find $1 -type f | sed 's/ /\\ /g' ); do
This does want I want but the problem is the the whitespace char, so "Hello\ World" will be 1. "Hello\" and 2. "World" in the loop. Any way in bash to make the for loop make i read the entire line?
Yes, I've tried quotes here too. Same problem.
Cheers
|
|
|
|
04-23-2006, 11:31 AM
|
#5
|
|
Member
Registered: Sep 2005
Location: Old Blighty
Distribution: Slackware, NetBSD
Posts: 536
Rep:
|
My usual recommendation is to use zsh, which doesn't do stupid things like automatic word splitting on scalar variables. Bash is a poor excuse for a shell, IMO.
|
|
|
|
04-24-2006, 05:21 PM
|
#6
|
|
Moderator
Registered: May 2001
Posts: 24,969
|
@colabus: the solution was handed to you by homey. The problem was obscured by the "for loop" expansion. Note that you best use "while loops" if you can't guesstimate what the expansion will amount to:
Code:
find $1 -type f|while read i; do md5sum -b "$i" >> $checksumDir/$2.$3; done
@ioerror:
Quote:
|
My usual recommendation is to use zsh
|
That's why all the OSes install Zsh as default shell and all the "important" apps are written as or ported to zsh shell scripts, right?.. Not.
Last edited by unSpawn; 04-24-2006 at 05:23 PM.
Reason: //have keybd, can't type
|
|
|
|
04-24-2006, 05:33 PM
|
#7
|
|
Member
Registered: Sep 2005
Location: Old Blighty
Distribution: Slackware, NetBSD
Posts: 536
Rep:
|
Quote:
|
That's why all the OSes install Zsh as default shell
|
Er, zsh is the default shell on MacOS X. NetBSD has it's own shell, other "OSes" use other shells, etc. Bash is the default shell on Linux because bash is the GNU shell, and Linux systems are GNU systems. So what? Any complete distribution will include zsh, tcsh, ksh etc. A distro that only installs bash is one I wouldn't touch with barge pole.
Quote:
|
the "important" apps are written as or ported to zsh shell scripts, right?
|
I thought that "important" apps were written in C.
There is nothing magical about bash, it is just the GNU shell. I don't think it's very good, it has lots of stupidities, like automatic word splitting. So I use zsh, and I merely suggested that the querent give it a try since it will automatically solve his problem. What's wrong with that?
|
|
|
|
04-24-2006, 07:35 PM
|
#8
|
|
Moderator
Registered: May 2001
Posts: 24,969
|
I thought that "important" apps were written in C.
Yeah, I should probably rephrase that as: "there are more popular and commonly used tools written in Bash compared to Zsh".
like automatic word splitting.
See IFS variable.
I merely suggested that the querent give it a try since it will automatically solve his problem. What's wrong with that?
I'm not questioning your competence but, considering the "fix" is that trivial, substituting Bash for zsh is utterly unnecessary.
|
|
|
|
04-25-2006, 05:27 AM
|
#9
|
|
Senior Member
Registered: Nov 2005
Location: Belgium
Distribution: Red Hat, Fedora
Posts: 1,515
Rep:
|
@colabus:
to summarize the discussion between ioerror and unSpawn:
set $IFS prior to your for-loop.
Give it a value like "\n" (newline) only.
This should make the for-loop avoid splitting the words (shell splits up the words on spaces, tabs and newlines by default) that come out of the "find" command.
See:
and search for "IFS".
|
|
|
|
04-25-2006, 06:06 AM
|
#10
|
|
Member
Registered: Jan 2005
Posts: 178
Rep:
|
Why not use find's `-exec' command?
|
|
|
|
04-25-2006, 06:14 AM
|
#11
|
|
Senior Member
Registered: Nov 2005
Location: Belgium
Distribution: Red Hat, Fedora
Posts: 1,515
Rep:
|
Good point, H. Tsu.
An alternative would be to use the -print0 option of "find" and to pipe it into xargs -0.
I've seen solutions like that around on this forum too.
|
|
|
|
04-25-2006, 06:18 AM
|
#12
|
|
Member
Registered: Jan 2005
Posts: 178
Rep:
|
Or
for i in "$(find . -maxdepth 1 -type f)"; do md5sum "$i"; done
|
|
|
|
04-25-2006, 06:48 AM
|
#13
|
|
Member
Registered: Jan 2005
Posts: 178
Rep:
|
Also
find . -type f | while read line; do md5sum "$line"; done
Note newline char is "legal" in filename, so this one may not work properly.
The best solution I think is
find . -type f -print0 | xargs --null md5sum
Last edited by T.Hsu; 04-25-2006 at 06:53 AM.
|
|
|
|
04-25-2006, 09:50 AM
|
#14
|
|
Member
Registered: Sep 2005
Location: Old Blighty
Distribution: Slackware, NetBSD
Posts: 536
Rep:
|
Quote:
|
there are more popular and commonly used tools written in Bash compared to Zsh
|
Well, bash is more prevalent (at least on Linux), so it's natural that more people would use it, but zsh could probably run most of those scripts without modification, although it does have a few subtle incompatibilities with POSIX syntax.
Quote:
|
I'm not questioning your competence but, considering the "fix" is that trivial, substituting Bash for zsh is utterly unnecessary.
|
OK, for a single case I would agree, but personally I haven't used bash for years, so no substitution occurs!
I don't want to start a pointless bash vs. zsh debate but zsh has lots of other features (e.g. nested parameter expansion (which bash still can't do), associative arrays, etc) which makes it an excellent choice both for scripting and interactive use. OK, I'm going to stop now. I only mentioned it in the first place because bash is so prevalent and the lesser known shells often get overlooked. After all, Linux is all about choice...
|
|
|
|
04-25-2006, 10:03 AM
|
#15
|
|
Moderator
Registered: May 2001
Posts: 24,969
|
I don't want to start a pointless bash vs. zsh debate
Me neither.
but zsh has lots of other features (e.g. nested parameter expansion (which bash still can't do), associative arrays, etc)
Yes, you're right there. Bash' array-fu certainly isn't that evolved.
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 09:07 AM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|