Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Mar 24th, 2008, 6:58 PM   #1
estergon
Newbie
 
estergon's Avatar
 
Join Date: Mar 2008
Posts: 8
Rep Power: 0 estergon is on a distinguished road
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
__________________
ESTRAGON: We've lost our rights?
VLADIMIR: (distinctly) We got rid of them.
estergon is offline   Reply With Quote
Old Mar 24th, 2008, 7:42 PM   #2
Jessehk
The Oblivious One
 
Jessehk's Avatar
 
Join Date: May 2005
Location: Ontario, Canada
Posts: 648
Rep Power: 4 Jessehk is on a distinguished road
Re: Spaces in path names

bash Syntax (Toggle Plain Text)
  1. HDA1="/mnt/hda1/Documents and Settings"

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

__________________
Dr. Zoidberg: [ecstatic] I'm going to a movie... with FRIENDS!
Jessehk is offline   Reply With Quote
Old Mar 24th, 2008, 7:47 PM   #3
estergon
Newbie
 
estergon's Avatar
 
Join Date: Mar 2008
Posts: 8
Rep Power: 0 estergon is on a distinguished road
Re: Spaces in path names

Quote:
Originally Posted by Jessehk View Post
bash Syntax (Toggle Plain Text)
  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?
__________________
ESTRAGON: We've lost our rights?
VLADIMIR: (distinctly) We got rid of them.
estergon is offline   Reply With Quote
Old Mar 24th, 2008, 8:09 PM   #4
Sane
Banned
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 2,101
Rep Power: 6 Sane will become famous soon enough
Send a message via MSN to Sane
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"
__________________
Looking for tough programming challenges? Try participating in Sane's Monthly Algorithms Challenges!
Composing Techno is a little side hobby of mine. Techno by DJ Sane. All free for download.
Sane is offline   Reply With Quote
Old Mar 24th, 2008, 8:13 PM   #5
estergon
Newbie
 
estergon's Avatar
 
Join Date: Mar 2008
Posts: 8
Rep Power: 0 estergon is on a distinguished road
Re: Spaces in path names

Quote:
Originally Posted by Sane View Post
I'll take a whack at guessing:

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

HDA1="/mnt/hda1/Documents\\ and\\ Settings"
Sorry to say no
$ 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..
__________________
ESTRAGON: We've lost our rights?
VLADIMIR: (distinctly) We got rid of them.
estergon is offline   Reply With Quote
Old Mar 24th, 2008, 8:53 PM   #6
Jabo
Not a user?
 
Join Date: Sep 2007
Posts: 308
Rep Power: 2 Jabo is on a distinguished road
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.

Last edited by Jabo; Mar 24th, 2008 at 9:04 PM.
Jabo is offline   Reply With Quote
Old Mar 24th, 2008, 9:00 PM   #7
Seif
Hobbyist Programmer
 
Seif's Avatar
 
Join Date: Jan 2006
Location: UK
Posts: 244
Rep Power: 3 Seif is on a distinguished road
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
Seif is offline   Reply With Quote
Old Mar 24th, 2008, 9:18 PM   #8
estergon
Newbie
 
estergon's Avatar
 
Join Date: Mar 2008
Posts: 8
Rep Power: 0 estergon is on a distinguished road
Re: Spaces in path names

Quote:
Originally Posted by Jabo View Post
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
Thanks for the link by the way .
__________________
ESTRAGON: We've lost our rights?
VLADIMIR: (distinctly) We got rid of them.
estergon is offline   Reply With Quote
Old Mar 24th, 2008, 9:22 PM   #9
estergon
Newbie
 
estergon's Avatar
 
Join Date: Mar 2008
Posts: 8
Rep Power: 0 estergon is on a distinguished road
Re: Spaces in path names

Quote:
Originally Posted by Seif View Post
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 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?!

Thanks you all for your interest
I will be right back
__________________
ESTRAGON: We've lost our rights?
VLADIMIR: (distinctly) We got rid of them.
estergon is offline   Reply With Quote
Old Mar 24th, 2008, 9:48 PM   #10
estergon
Newbie
 
estergon's Avatar
 
Join Date: Mar 2008
Posts: 8
Rep Power: 0 estergon is on a distinguished road
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
__________________
ESTRAGON: We've lost our rights?
VLADIMIR: (distinctly) We got rid of them.
estergon is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Removing double spaces with str_replace davil PHP 2 Dec 12th, 2007 6:19 PM
adding csc to Path Environment pr0gm3r C# 6 Sep 9th, 2005 9:52 AM
seeking best path algorithm lhk_mgtf2008 Visual Basic 9 Jun 9th, 2005 9:11 PM
How create variables to hold directory names on Windows? noleander Bash / Shell Scripting 5 Mar 7th, 2005 11:18 AM
MYSQL, pipe delimited or just spaces? scorpiosage PHP 0 Feb 8th, 2005 4:32 AM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 6:30 AM.

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