LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Paths and escape characters (http://www.linuxquestions.org/questions/linux-newbie-8/paths-and-escape-characters-462962/)

code-breaker 07-11-2006 09:34 AM

Paths and escape characters
 
I am using ubuntu, and I am writing a bash script. I am quite new to linux, and this is my first real script.

This script will run from the context menu in nautilus. I get the path of the current directory in the variable $NAUTILUS_SCRIPT_CURRENT_URI, and I need to strip off the last folder name. I use:
Code:

folder1=`basename "$NAUTILUS_SCRIPT_CURRENT_URI"`
or
Code:

folder1="${NAUTILUS_SCRIPT_CURRENT_URI##*/}"
and these work, so that if:
Code:

$NAUTILUS_SCRIPT_CURRENT_URI = home/this/that
then
Code:

$folder1 = that
...but if there are any spaces in the last folder, they show up as %20. How do I 'unescape' the spaces? I have tried:
Code:

folder2 = ${folder1//%20/\ }
But no dice. Any suggestions?

hazmatt20 07-11-2006 09:45 AM

What do you get if you try this?
Code:

$folder1 = that|sed 's/%20/\ /g'

code-breaker 07-11-2006 09:59 AM

Code:

album="${NAUTILUS_SCRIPT_CURRENT_URI##*/}"
$album = $album|sed 's/%20/\ /g'
kdialog --title "Album:" \
      --msgbox $album

Gives me the %20's. Any other suggestions? Am I doing something wrong?

muha 07-11-2006 01:39 PM

Maybe they are still spaces when you are handling them?
Try replacing them with underscores if that is an option:
Code:

$folder1 = that|sed 's#\ #_#g'

code-breaker 07-11-2006 11:24 PM

I got it working, although I don't really understand why.

Code:

a=`basename "$NAUTILUS_SCRIPT_CURRENT_URI"`
folder=${a//\%20/\ }
kdialog --title "folder:" \
      --msgbox "$folder"

Works, and the only difference I can see from my original attempt is that I wasn't escaping the '%', but I could have sworn that I did in 1 of the 1000 syntax permutations I tried.

Also, is there a problem with putting a space before the '='?

Thanks for the help.


All times are GMT -5. The time now is 05:04 PM.