![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
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
|
|
|||||
|
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 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 |
|
||||
|
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}"
Code:
A A B B C C VAR= Why not? |
|
|||||
|
Quote:
|
|
||||
|
Quote:
Oh, well. I may have stumbled upon a different solution: Code:
$!/bin/sh VAR="$(ls | tr '\n' :)" echo "VAR=$VAR" Code:
VAR=A A:B B:C C: Code:
#!/bin/sh
while [ -n "$VAR" ]; do
f="${VAR%%:*}"
VAR="${VAR#*:}"
# process "$f" ...
done
|
|
|||||
|
Quote:
Quote:
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|