LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
LinkBack Search this Thread
Old 02-26-2013, 09:54 AM   #1
Don_Nadie
Member
 
Registered: Aug 2009
Posts: 36

Rep: Reputation: 0
[SOLVED]How do I specify a Windows path containing spaces to bash?


Linuxmint 14 Cinnamon 32 bit

I'm trying to use this bash script with gnome-schedule but bash barfs on the line which begins with DIR=. It's the folder on my mounted Windows partition which contains hundreds of wallpaper images.
Code:
#!/bin/bash

# Script to randomly set Background from files in a directory

# Directory Containing Pictures
DIR='/mnt/XXXXX/Documents and Settings/All Users/Documents/My Pictures/Wallpaper'

# Command to Select a random jpg file from directory
# Delete the *.jpg to select any file but it may return a folder
PIC=$(ls $DIR/*.jpg | shuf -n1)

# Command to set Background Image
gconftool -t string -s /desktop/gnome/background/picture_filename $PIC
I've tried
Code:
DIR='/mnt/XXXXX/Documents and Settings/All Users/Documents/My Pictures/Wallpaper'
Code:
DIR='/mnt/XXXXX/Documents\ and\ Settings/All\ Users/Documents/My\ Pictures/Wallpaper'
Code:
DIR="/mnt/XXXXX/Documents and Settings/All Users/Documents/My Pictures/Wallpaper"
Code:
DIR="/mnt/XXXXXo/Documents\ and\ Settings/All\ Users/Documents/My\ Pictures/Wallpaper"
but bash can never handle the space characters. What's the secret? TNX.

Last edited by Don_Nadie; 03-03-2013 at 10:56 AM. Reason: Solved
 
Old 02-26-2013, 10:05 AM   #2
suicidaleggroll
Senior Member
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 2,426

Rep: Reputation: 890Reputation: 890Reputation: 890Reputation: 890Reputation: 890Reputation: 890Reputation: 890
What do you mean it "barfs"?

Code:
$ DIR='/mnt/XXXXX/Documents and Settings/All Users/Documents/My Pictures/Wallpaper'
$ echo $DIR
/mnt/XXXXX/Documents and Settings/All Users/Documents/My Pictures/Wallpaper
$
If I had to guess I'd say the assignment is working fine, it's the way you're using it that's messing things up. The "ls $DIR/*.jpg" will most certainly break the way you have it written, you need to put the $DIR in quotes to keep ls from splitting on the spaces. You probably will need to put the $PIC in quotes as well in the call to gconftool to keep it from splitting on the spaces as well.

Last edited by suicidaleggroll; 02-26-2013 at 10:11 AM.
 
Old 02-26-2013, 10:58 AM   #3
lleb
Senior Member
 
Registered: Dec 2005
Location: Florida
Distribution: CentOS/Fedora
Posts: 2,299

Rep: Reputation: 395Reputation: 395Reputation: 395Reputation: 395
well without the exact error you are getting i can only guess that your path for the DIR should be without the ' ' or " " and the option with the \ foo should work.

Code:
DIR=/mnt/XXXXXo/Documents\ and\ Settings/All\ Users/Documents/My\ Pictures/Wallpaper
then when you call DIR later call it with squiggly brackets... ${DIR} same with when you call PIC... ${PIC} not sure if that helps, but hope it does.
 
Old 02-26-2013, 11:31 AM   #4
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Hanover, Germany
Distribution: Gentoo, Slackware
Posts: 14,896
Blog Entries: 2

Rep: Reputation: 3810Reputation: 3810Reputation: 3810Reputation: 3810Reputation: 3810Reputation: 3810Reputation: 3810Reputation: 3810Reputation: 3810Reputation: 3810Reputation: 3810
You shouldn't parse ls output, but anyways, the real problem here is that you are not quoting the variable, replace the line
Code:
PIC=$(ls $DIR/*.jpg | shuf -n1)
with
Code:
PIC=$(ls "$DIR"/*.jpg | shuf -n1)
and the line
Code:
gconftool -t string -s /desktop/gnome/background/picture_filename $PIC
with
Code:
gconftool -t string -s /desktop/gnome/background/picture_filename "$PIC"
to escape the spaces.
 
2 members found this post helpful.
Old 02-26-2013, 11:41 AM   #5
lleb
Senior Member
 
Registered: Dec 2005
Location: Florida
Distribution: CentOS/Fedora
Posts: 2,299

Rep: Reputation: 395Reputation: 395Reputation: 395Reputation: 395
TobiSGD was i correct on the variable setting not needing quotes around the path? still learning.
 
Old 02-26-2013, 11:45 AM   #6
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Hanover, Germany
Distribution: Gentoo, Slackware
Posts: 14,896
Blog Entries: 2

Rep: Reputation: 3810Reputation: 3810Reputation: 3810Reputation: 3810Reputation: 3810Reputation: 3810Reputation: 3810Reputation: 3810Reputation: 3810Reputation: 3810Reputation: 3810
Quote:
Originally Posted by lleb View Post
TobiSGD was i correct on the variable setting not needing quotes around the path? still learning.
You can use both ways (quotes or backslashes) to escape the spaces, I am not quite sure if the curly braces are enough to escape them when the variables are referenced later or if you need quotes there, too.
I am sure soon someone with better Bash knowledge than me will chime in here.
 
1 members found this post helpful.
Old 02-26-2013, 12:13 PM   #7
lleb
Senior Member
 
Registered: Dec 2005
Location: Florida
Distribution: CentOS/Fedora
Posts: 2,299

Rep: Reputation: 395Reputation: 395Reputation: 395Reputation: 395
thanks. always trying to learn.
 
Old 03-03-2013, 11:00 AM   #8
Don_Nadie
Member
 
Registered: Aug 2009
Posts: 36

Original Poster
Rep: Reputation: 0
Sorry for the delay in getting back to this post

Here's what I've got now
Code:
#!/bin/bash

# Script to randomly set Background from files in a directory

# Directory Containing Pictures
DIR='/mnt/XXXXX/Documents and Settings/All Users/Documents/My Pictures/Wallpaper'

# Command to Select a random jpg file from directory
# Delete the *.jpg to select any file but it may return a folder
PIC=$(ls "$DIR"/*.jpg | shuf -n1)

# Command to set Background Image
#gconftool -t string -s /desktop/gnome/background/picture_filename "$PIC"
gsettings set org.gnome.desktop.background picture-uri file:"$PIC"
It works now. The gconftool command no longer works, but that's a Gnome thing.

Thanks to everyone who replied to my post. I hope this will be of help to others. I'm going to use the script with the gnome-schedule command to change my desktop background once a day at a given time.
 
  


Reply

Tags
bash, spaces


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[Bash] How to expand path variable that contains spaces and wildcards jkv Programming 11 02-17-2010 12:19 AM
passing variable (a path) with spaces aolong Linux - General 2 06-28-2009 05:50 PM
How to write a the baseurl of a path that has spaces davidmuraya Linux - Newbie 8 09-10-2008 03:13 AM
Xargs and spaces in file path mgichoga Linux - Software 2 01-04-2008 12:57 PM
makefiles with spaces in path teuf Programming 7 08-30-2007 03:04 AM


All times are GMT -5. The time now is 08:45 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration