Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Bash / Shell Scripting (http://www.programmingforums.org/forum26.html)
-   -   Spaces in path names (http://www.programmingforums.org/showthread.php?t=15474)

estergon Mar 24th, 2008 5:58 PM

Spaces in path names
 
Hello Everyone,
I am new here and just started shell scripting.
I thought I would start with creating a shell script as a solution to one of my problems.
I need some help with variables and path names (especially spaces in pathnames) here.
I am trying to use my firefox bookmarks on linux with that on windows synchronously but I am getting errors with the path variable for the windows profile folder since it has spaces in its pathname.
For Example:
:

$ cd /mnt/hda1/Documents\ and\ Settings/
will work, where
:

$ HDA1="/mnt/hda1/Documents\ and\ Settings/"
$ cd $HDA1

will give such error:
:

bash: cd: /mnt/hda1/Documents\: No such file or directory

With/out quotes, backslashes, all same.. I understand that shell does not like spaces. What should I write instead since I cannot change the pathname? Any suggestions please?

Thank in advance

Jessehk Mar 24th, 2008 6:42 PM

Re: Spaces in path names
 
:

  1. HDA1="/mnt/hda1/Documents and Settings"


Since everything is "encased" in the quotation marks, spaces shouldn't be a problem.

:)

estergon Mar 24th, 2008 6:47 PM

Re: Spaces in path names
 
Quote:

Originally Posted by Jessehk (Post 142909)
:

  1. HDA1="/mnt/hda1/Documents and Settings"


Since everything is "encased" in the quotation marks, spaces shouldn't be a problem.

:)

Thank a lot for the quick reply :)
I already tried that but I am getting same thing.. :(
:

$ HDA1="/mnt/hda1/Documents and Settings"
$ cd $HDA1
bash: cd: /mnt/hda1/Documents: No such file or directory

Any other suggestins? :?:

Sane Mar 24th, 2008 7:09 PM

Re: Spaces in path names
 
I'll take a whack at guessing:

:

HDA1="\"/mnt/hda1/Documents and Settings\""

Or:

:

HDA1="/mnt/hda1/Documents\\ and\\ Settings"

estergon Mar 24th, 2008 7:13 PM

Re: Spaces in path names
 
Quote:

Originally Posted by Sane (Post 142912)
I'll take a whack at guessing:

:

HDA1="\"/mnt/hda1/Documents and Settings\""
Or:

:

HDA1="/mnt/hda1/Documents\\ and\\ Settings"

Sorry to say no :S
:

$ HDA1="\"/mnt/hda1/Documents and Settings\""
$ echo $HDA1
"/mnt/hda1/Documents and Settings"
$ cd $HDA1
bash: cd: "/mnt/hda1/Documents: No such file or directory


:

$ HDA1="/mnt/hda1/Documents\\ and\\ Settings"
$ echo $HDA1
/mnt/hda1/Documents\ and\ Settings
$ cd $HDA1
bash: cd: /mnt/hda1/Documents\: No such file or directory


Keep it coming please.. I am sure we will work this out by the sun rise.. ;)

Jabo Mar 24th, 2008 7:53 PM

Re: Spaces in path names
 
It may have something to do with whether or not you're using single or double quotes if you have variables in your script somewhere that have a name in your directory path. If so, you will have to either use single quotes or use an escape character to ignore the variable. More Info

:

$ HDA1="\"/mnt/hda1/Documents and Settings\""
$ echo $HDA1
"/mnt/hda1/Documents and Settings"
$ cd $HDA1
bash: cd: "/mnt/hda1/Documents: No such file or directory


It may be since you are using HDA1 as a variable, and hda1 is in your path, that you may have to escape the hda1 to get it to work; or use single quotes.

Seif Mar 24th, 2008 8:00 PM

Re: Spaces in path names
 
a shot in the dark but change jessehks:

:

$ HDA1="/mnt/hda1/Documents and Settings"
$ cd $HDA1


to:

:

$ HDA1="/mnt/hda1/Documents and Settings"
$ cd "$HDA1"


?

has been a while since i've written a shell script

estergon Mar 24th, 2008 8:18 PM

Re: Spaces in path names
 
Quote:

Originally Posted by Jabo (Post 142914)
It may have something to do with whether or not you're using single or double quotes if you have variables in your script somewhere that have a name in your directory path. If so, you will have to either use single quotes or use an escape character to ignore the variable. More Info

:

$ HDA1="\"/mnt/hda1/Documents and Settings\""
$ echo $HDA1
"/mnt/hda1/Documents and Settings"
$ cd $HDA1
bash: cd: "/mnt/hda1/Documents: No such file or directory


It may be since you are using HDA1 as a variable, and hda1 is in your path, that you may have to escape the hda1 to get it to work; or use single quotes.


I tried all possible combinations with double quotes, single quote, slashs, backslash in defining the variable all same: linefeeds after space. :(
I read that
-1 (digit one, not lower L) parameter may prevent new line feed for ls command but I could not implement it to cp or mv commands :S
Thanks for the link by the way .

estergon Mar 24th, 2008 8:22 PM

Re: Spaces in path names
 
Quote:

Originally Posted by Seif (Post 142916)
a shot in the dark but change jessehks:

:

$ HDA1="/mnt/hda1/Documents and Settings"
$ cd $HDA1


to:

:

$ HDA1="/mnt/hda1/Documents and Settings"
$ cd "$HDA1"


?

Has been a while since i've written a shell script

Oh that's it :D It worked! Thanks a lot. So the key was to use double quotes while calling the variable as well as while defining the variable, eh?! :lol:

Thanks you all for your interest :)
I will be right back :lol:

estergon Mar 24th, 2008 8:48 PM

Re: Spaces in path names
 
I just wanted to share my final script which works just fine.
Since the installed add-ons are not compatible for both linux and windows at a time, using same profile folder with -ProfileManager was a bit of useless thus I really needed this thing:

:

#!/bin/sh
DATE=`date +%F-%H%M%S`
HDA1="/mnt/hda1/Documents and Settings/username/Application Data/Mozilla/Firefox/Profiles/profile0.default"
HDA6=/home/user/.mozilla/firefox/profile1.default
mv -v $HDA6/bookmarks.html $HDA6/bookmarkbackups/bookmarks-$DATE.html
cp -v "$HDA1"/bookmarks.html "$HDA1"/bookmarkbackups/bookmarks$DATE.html
mv -v "$HDA1"/bookmarks.html $HDA6/bookmarks.html
firefox
cp -v $HDA6/bookmarks.html "$HDA1"/bookmarks.html


I hope it is useful for somebody else out there too :)


All times are GMT -5. The time now is 2:12 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC