The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
spaces in filenames Bab00shka Shell Programming and Scripting 8 09-30-2008 03:13 PM
looping thru filenames in a directory silas.john Shell Programming and Scripting 1 07-02-2008 09:27 AM
spaces in filenames, for do naviztirf Shell Programming and Scripting 4 10-17-2007 06:08 PM
how to handle spaces in filenames rayne Shell Programming and Scripting 4 11-19-2006 01:37 PM
spaces in filenames Hitori Shell Programming and Scripting 4 07-04-2006 05:35 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 11-28-2008
KenJackson KenJackson is offline
Registered User
  
 

Join Date: May 2008
Location: Maryland, USA
Posts: 107
Looping through filenames with spaces

I need to loop through the files in a directory and process the files. But some of the filenames contain spaces.

Here is a little test script I've been using to experiment. (I'm not really going to call 'echo', I'm doing some other processing.) Everything I try fails. How can I do this??
Code:
#!/bin/sh
# spacetest
DIR="/tmp/spacetest$$"
rm -rf $DIR
mkdir -p $DIR || exit 1
touch $DIR/{"a b","c d"}

# This works, when I itemize filenames a priori
set "a b" "c d"
for x in "$@"; do
    echo $x
done

echo Test 1:    # Fails
for x in $(find $DIR -mindepth 1 -printf '"%f" '); do
    echo $x
done

echo Test 2:    # Fails
set $(find $DIR -mindepth 1 -printf '%f ')
for x in "$@"; do
    echo $x
done

echo Test 3:    # Fails
set $(find $DIR -mindepth 1 -printf '"%f" ')
for x in "$@"; do
    echo $x
done

echo Test 4:    # Fails
_list="$(find $DIR -mindepth 1 -printf '"%f" ')"
set $_list
for x in "$@"; do
    echo $x
done

rm -rf $DIR
  #2 (permalink)  
Old 11-28-2008
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,361
Quote:
Originally Posted by KenJackson View Post
I need to loop through the files in a directory and process the files. But some of the filenames contain spaces.
...
Code:
#!/bin/sh
# spacetest
DIR="/tmp/spacetest$$"
####
echo Test 1:    # Fails
for x in $(find $DIR -mindepth 1 -printf '"%f" '); do
    echo $x
done

Assuming there are no newlines in any filenames:

Code:
find $DIR -mindepth 1 -printf '"%f" ' |
while IFS= read -r file
do
  echo "$x"
done
  #3 (permalink)  
Old 11-28-2008
joeyg's Avatar
joeyg joeyg is offline Forum Staff  
modérateur
  
 

Join Date: Dec 2007
Location: Home of 17-time world champion Boston Celtics
Posts: 1,311
Wink A couple of thoughts

use of quotes and use of IFS

Code:
> cat see_blanks
ls -l file* 
ls | grep "file" >f_list

while read filename
  do
  echo "### echo"
  echo $filename
  echo "### ls"
  ls $filename
  echo "### ls in quotes"
  ls "$filename"
  echo "### play with IFS"
  OLDIFS=$IFS
  IFS="~"
  ls $filename
  IFS=$OLDIFS

done<f_list
prorgam execution

Code:
> see_blanks
-rw-rw---- 1 xxx dp 0 Nov 28 10:04 file 04
-rw-rw---- 1 xxx dp 0 Nov 28 10:04 file01
### echo
file 04
### ls
ls: cannot access file: No such file or directory
ls: cannot access 04: No such file or directory
### ls in quotes
file 04
### play with IFS
file 04
### echo
file01
### ls
file01
### ls in quotes
file01
### play with IFS
file01
So, if I use the variable inside quotes, it works. And if I reset the IFS setting then I do not break-apart the commands at each space.
  #4 (permalink)  
Old 01-07-2009
KenJackson KenJackson is offline
Registered User
  
 

Join Date: May 2008
Location: Maryland, USA
Posts: 107
I made some progress with input from this forum, thank you, but I'm down to one stick point. Consider this simpler script:
Code:
#!/bin/sh
VAR=
echo 'A A
B B
C C' | while read f; do
    VAR="$VAR:$f"
    echo $f
done
echo "VAR=${VAR}"
The output is
Code:
A A
B B
C C
VAR=
The f variable echos properly in the loop, but I just can't seem to assign it's value to any other variable.
Why not?
  #5 (permalink)  
Old 01-07-2009
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,361
Quote:
Originally Posted by KenJackson View Post
I made some progress with input from this forum, thank you, but I'm down to one stick point. Consider this simpler script:
Code:
#!/bin/sh
VAR=
echo 'A A
B B
C C' | while read f; do
    VAR="$VAR:$f"
    echo $f
done
echo "VAR=${VAR}"
The output is
Code:
A A
B B
C C
VAR=
The f variable echos properly in the loop, but I just can't seem to assign it's value to any other variable.
Why not?

Each element of a pipeline is executed in its own subshell and therefore cannot affect the parent shell.

Try echo "$VAR" inside the loop, and you'll see that it does get assigned to.
  #6 (permalink)  
Old 01-08-2009
KenJackson KenJackson is offline
Registered User
  
 

Join Date: May 2008
Location: Maryland, USA
Posts: 107
Quote:
Originally Posted by cfajohnson View Post
Each element of a pipeline is executed in its own subshell and therefore cannot affect the parent shell.
Oh. How terribly inconvenient.

Oh, well. I may have stumbled upon a different solution:
Code:
$!/bin/sh
VAR="$(ls | tr '\n' :)"
echo "VAR=$VAR"
If I have files "A A", "B B" and "C C", I'll get this result:
Code:
VAR=A A:B B:C C:
Now I can work with VAR like this:
Code:
#!/bin/sh
while [ -n "$VAR" ]; do
    f="${VAR%%:*}"
    VAR="${VAR#*:}"
    # process "$f" ...
done
  #7 (permalink)  
Old 01-08-2009
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,361
Quote:
Originally Posted by KenJackson View Post
Oh. How terribly inconvenient.

Oh, well. I may have stumbled upon a different solution:
Code:
$!/bin/sh
VAR="$(ls | tr '\n' :)"
echo "VAR=$VAR"

You don't need any external commands to do that:

Code:
VAR=$( printf "%s:" * )
In bash, 3.1 and later, you can do it with:

Code:
printf -v VAR "%s:" *
Quote:
If I have files "A A", "B B" and "C C", I'll get this result:
Code:
VAR=A A:B B:C C:
Now I can work with VAR like this:
Code:
#!/bin/sh
while [ -n "$VAR" ]; do
    f="${VAR%%:*}"
    VAR="${VAR#*:}"
    # process "$f" ...
done

What's wrong with:

Code:
for f in *
do
    : # process "$f" ...
done
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate 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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 10:49 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0