Linux - NewbieThis 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.
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.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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?
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.
@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:
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?
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.
@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.
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...
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.