Unix Shell - handling spaces in the filenames of a URL

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > September 2006 > handling spaces in the filenames of a URL





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author handling spaces in the filenames of a URL
osiris@abydos.kmt

2006-09-25, 1:52 am

I am using /usr/bin/find to produce a list of complete file specifications
on which to operate in a while loop. Each file is in the hierarchy
of a website.

For each web page that I create, I create a navigator bar
near the top of the web page which allows the surfer
to navigate back up the path to the home page (as is commonly done).

The function that I wrote to accomplish this works just as I wanted
with one flaw. It will fail if it finds a space in the complete
filespec. Originally I wrote this just for my own use (I generally
don't use spaces in filenames but I now want to adapt it to anyone
using it). I can used sed to escape all the spaces which works fine.
However, if you check the code below, those escaped spaces don't
work so well when I use my "$SED_PATH" to determine the depth
of the directory in question. How can I modify this to find
the depth without replacing directory delimiters (slashes)
with spaces as I have done. Okay, it was a mistake but it worked.

Thanks for any help.

The variable $BASEPATH below is the document root of the website.

create_navigator()
{
# 1) Declare Variables
BASEPATH_PARENT="${BASEPATH%/*}"
SED_BASENAME=`echo "$BASEPATH_PARENT" |sed 's@\/@ @g'`
SED_FULLSPEC=`echo "$CURRENT_DIR_FULLSPEC" |sed 's@\/@ @g'`
SED_PATH=`echo "$SED_FULLSPEC" |sed "s@${SED_BASENAME}@@g"`

# 2) Start HTML table row
printf "%s\n" '<tr>'
printf "%s\n" '<td align=left NOWRAP bgcolor=#ffffff width=100%'
printf "%s\n" ' colspan=2>'

# 3) Devise the URLs for page navigation
NEW_PATH=""
parent="../"
I=1
DEPTH=`echo "$SED_PATH" |wc -w`
for name in $SED_PATH ; do
J=$I
while [ "$J" -lt "$DEPTH" ] ; do
name="${parent}$name"
J=`echo "$J" +1 |bc`
done
I=`echo "$I" +1 |bc`
NEW_PATH="${NEW_PATH} $name"
done

# 4) Create the HTML links for navigation
I=1
DEPTH=`echo "$NEW_PATH" |wc -w`
for url in $NEW_PATH ; do
TEXT=`echo "$url" |sed 's/..\///g'`
if [ "$I" -lt "$DEPTH" ] ; then
printf "%s%s%s" '<a href="' "$url" '">'
printf "%s%s" ' <font face="' 'comic sans ms'
printf "%s" '" size=+1 color=purple>'
printf "%s" "<bold>$TEXT"
printf "%s" '</bold></font>'
printf "%s\n" "</a>&nbsp;&nbsp;>&nbsp;"
else
printf "%s%s" ' <font face="' 'comic sans ms'
printf "%s" '" size=+1 color=purple>'
printf "%s" "$TEXT"
printf "%s" '</font>'
fi
I=`echo "$I" +1 |bc`
done
printf "\n"

# 5) Complete the HTML table row
printf "%s\n" '</td>'
printf "%s\n" '</tr>'
printf "\n"
}
Janis Papanagnou

2006-09-25, 8:15 am

osiris@abydos.kmt wrote:
> I am using /usr/bin/find to produce a list of complete file specifications
> on which to operate in a while loop. Each file is in the hierarchy
> of a website.
>
> For each web page that I create, I create a navigator bar
> near the top of the web page which allows the surfer
> to navigate back up the path to the home page (as is commonly done).


That's what CSS (style sheets) are for; define once, use everywhere.

>
> The function that I wrote to accomplish this works just as I wanted
> with one flaw. It will fail if it finds a space in the complete
> filespec.


To represent spaces in URIs you need to use their alternative form %20.

> Originally I wrote this just for my own use (I generally
> don't use spaces in filenames but I now want to adapt it to anyone
> using it). I can used sed to escape all the spaces which works fine.
> However, if you check the code below, those escaped spaces don't
> work so well when I use my "$SED_PATH" to determine the depth
> of the directory in question. How can I modify this to find
> the depth without replacing directory delimiters (slashes)
> with spaces as I have done. Okay, it was a mistake but it worked.
>
> Thanks for any help.
>
> The variable $BASEPATH below is the document root of the website.
>
> create_navigator()
> {
> # 1) Declare Variables
> BASEPATH_PARENT="${BASEPATH%/*}"
> SED_BASENAME=`echo "$BASEPATH_PARENT" |sed 's@\/@ @g'`
> SED_FULLSPEC=`echo "$CURRENT_DIR_FULLSPEC" |sed 's@\/@ @g'`
> SED_PATH=`echo "$SED_FULLSPEC" |sed "s@${SED_BASENAME}@@g"`
>
> # 2) Start HTML table row
> printf "%s\n" '<tr>'
> printf "%s\n" '<td align=left NOWRAP bgcolor=#ffffff width=100%'
> printf "%s\n" ' colspan=2>'
>
> # 3) Devise the URLs for page navigation
> NEW_PATH=""
> parent="../"
> I=1
> DEPTH=`echo "$SED_PATH" |wc -w`
> for name in $SED_PATH ; do


Double quote $SED_PATH.

> J=$I
> while [ "$J" -lt "$DEPTH" ] ; do
> name="${parent}$name"
> J=`echo "$J" +1 |bc`


J=$(( $J + 1 ))

> done
> I=`echo "$I" +1 |bc`


dito

> NEW_PATH="${NEW_PATH} $name"


You'll generally have problems if you use spaces (or other characters
that may be also present in path names) between path components.

> done
>
> # 4) Create the HTML links for navigation
> I=1
> DEPTH=`echo "$NEW_PATH" |wc -w`
> for url in $NEW_PATH ; do
> TEXT=`echo "$url" |sed 's/..\///g'`
> if [ "$I" -lt "$DEPTH" ] ; then
> printf "%s%s%s" '<a href="' "$url" '">'
> printf "%s%s" ' <font face="' 'comic sans ms'
> printf "%s" '" size=+1 color=purple>'
> printf "%s" "<bold>$TEXT"
> printf "%s" '</bold></font>'
> printf "%s\n" "</a>&nbsp;&nbsp;>&nbsp;"
> else
> printf "%s%s" ' <font face="' 'comic sans ms'
> printf "%s" '" size=+1 color=purple>'
> printf "%s" "$TEXT"
> printf "%s" '</font>'
> fi


You may want to use here-documents for that purpose...

cat <<- EOT
.... Some text with $shell variables ...
EOT


Janis


> I=`echo "$I" +1 |bc`
> done
> printf "\n"
>
> # 5) Complete the HTML table row
> printf "%s\n" '</td>'
> printf "%s\n" '</tr>'
> printf "\n"
> }

osiris@abydos.kmt

2006-09-26, 1:47 am


On Mon, 25 Sep 2006 12:44:43 +0200, Janis Papanagnou <Janis_Papanagnou@hotmail.com> wrote:
>
>osiris@abydos.kmt wrote:
>
>That's what CSS (style sheets) are for; define once, use everywhere.


Thanks for your help, Janis. I appreciate your efforts.
I only briefly dabbled with a style sheet or two.
Can CSS really build a navigation bar though?

>
>To represent spaces in URIs you need to use their alternative form %20.


That's not really what I meant. I'm trying to separate the path
e.g.

/usr/local/bin/this directory/this subdirectory

into

usr > local > bin > 'this directory' > 'this subdirectory'

all in one function.


>
>Double quote $SED_PATH.


The for loop wouldn't work if I did so.
However, the for loop is actually the problem.
I just don't know how to change it into a while loop
and make it work so that I don't have to substitute
for the slashes in extracting each directory name.

>
>J=$(( $J + 1 ))
>
>
>dito
>
>
>You'll generally have problems if you use spaces (or other characters
>that may be also present in path names) between path components.


NEW_PATH is simply building the path of the navigator bar
which looks like:

usr > local > bin > 'this dir' > 'this subdir'


>
>You may want to use here-documents for that purpose...
>
>cat <<- EOT
>... Some text with $shell variables ...
>EOT
>
>
>Janis



[vbcol=seagreen]
>
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com