Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Issue With Filenames With Spaces In Them

Status
Not open for further replies.

btinder

Programmer
Mar 12, 2004
27
US
OK,
I searched and saw a couple of answers, but they didn't really apply to my script.

Here's a snippet from my script:

#!/bin/ksh

function build_ftp_script {
echo ${@:-} >> $ftp_script
}

function build_transfer_status {
if [[ -a $ftp_script ]] ; then
rm -f $ftp_script
fi

build_ftp_script "open $hostname"
# Get a list of files downloaded.
files=`/bin/ls incoming/tmp/*.zip 2>/dev/null`

for file in $files ; do
# Get the suffix.
echo $file
tmpfile=${file##incoming/tmp/}
build_ftp_script "rename ${tmpfile} ${tmpfile}.ftc"
done
}

build_transfer_status
exit 0


Now, if I encounter a .zip file with a space in it, it seperates the characters before and after the space into 2 seperate "files".

Any good ideas on how to avoid this? I've tried double quotes everywhere, nothing seems to work...
 
Replace this:
files=`/bin/ls incoming/tmp/*.zip 2>/dev/null`
for file in $files ; do
By this:
/bin/ls incoming/tmp/*.zip 2>/dev/null | while read file
do

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
UUOls
Code:
  for file in *.gz ; do
        # Get the suffix.
        echo "$file"
        tmpfile="${file##incoming/tmp/}"
        build_ftp_script "rename \"${tmpfile}\" \"${tmpfile}\".ftc"
  done

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Thanks PH,

That worked! Can you explain to me quickly why that way works and my way didn't?
 
Oops hit submit to fast !
And replace this:
build_ftp_script "rename ${tmpfile} ${tmpfile}.ftc"
By this:
build_ftp_script "rename \"${tmpfile}\" \"${tmpfile}.ftc\""

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
why that way works and my way didn't
for file in file is populated with each consecutive token separated by $IFS, so usually <Space>, <Tab> or <LineFeed>.
read file file is populated with each whole line

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top