| Author |
Spaces in directory names & script
|
|
| Poochieİ 2005-02-09, 2:48 am |
| Hi all,
I got this codetaken from a script of mine:
for dirname in *;
do
if [ -d $dirname ]
then
echo entrando in: $dirname
cd $dirname
[CUT]
what must I do to make it handle directories with whitespaces in their
name?
Very thanks in advance,
--
Poochieİ
| |
| Chris F.A. Johnson 2005-02-09, 2:48 am |
| On Wed, 09 Feb 2005 at 06:20 GMT, Poochieİ wrote:
> Hi all,
>
> I got this codetaken from a script of mine:
>
> for dirname in *;
> do
> if [ -d $dirname ]
> then
> echo entrando in: $dirname
> cd $dirname
>
> [CUT]
>
> what must I do to make it handle directories with whitespaces in their
> name?
Quote the dirnames:
for dirname in *;
do
if [ -d "$dirname" ]
then
echo entrando in: "$dirname"
cd "$dirname"
I'd rewrite the last two line to test for success of cd:
cd "$dirname" && echo entrando in: "$dirname" || : Error action here
--
Chris F.A. Johnson http://cfaj.freeshell.org/shell
========================================
===========================
My code (if any) in this post is copyright 2005, Chris F.A. Johnson
and may be copied under the terms of the GNU General Public License
| |
| Rakesh Sharma 2005-02-09, 7:48 am |
| Poochie=A9 wrote:
>
> I got this codetaken from a script of mine:
>
> for dirname in *;
> do
> if [ -d $dirname ]
> then
> echo entrando in: $dirname
> cd $dirname
>
> [CUT]
>
> what must I do to make it handle directories with whitespaces in
their
> name?
>
## Notes:i) prefix the wildcards with a ./ so that any funny
## characters(e.g., - ! etc.) are shielded from the commands
## where they are going to be used
## instead of 'echo' use either 'printf' or the << operator
## the wildcards are shown here to look for ``hidden'' directories
## also.
## use quotes around your variables to stop word splitting, IOW
## preserve spaces/TABS/newlines in your dir. names
## used 'd_name' as a variable name since 'dirname' is a command
## (although it's not a pbm. as such)
for d_name in ./* ./.[!.]* ./..?*;do
[ -d "${d_name}" ] || continue
cat - << [EOF]
entrando in: '${d_name}'
[EOF]
cd "${d_name}"
done
Or this:
for d_name in ./* ./.*;do
[ -d "${d_name}" ] || continue
case ${d_name} in './.'|'./..') continue;; esac
printf 'entrando in: "%s"\n' "${d_name}"
cd "${d_name}"
done
| |
| Poochieİ 2005-02-09, 5:56 pm |
| Chris F.A. Johnson <cfajohnson@gmail.com> wrote:
[CUT]
> Quote the dirnames:
[CUT]
Thanks to both. It actually works! My problem was with the TextEditor I
was using (TextWrangler under OSX) that printed some weird stylish
quotes instead of the default straight ones.
--
Poochieİ
|
|
|
|