LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   [SOLVED]How do I specify a Windows path containing spaces to bash? (http://www.linuxquestions.org/questions/linux-newbie-8/%5Bsolved%5Dhow-do-i-specify-a-windows-path-containing-spaces-to-bash-4175451766/)

Don_Nadie 02-26-2013 10:54 AM

[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.

suicidaleggroll 02-26-2013 11:05 AM

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.

lleb 02-26-2013 11:58 AM

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.

TobiSGD 02-26-2013 12:31 PM

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.

lleb 02-26-2013 12:41 PM

TobiSGD was i correct on the variable setting not needing quotes around the path? still learning.

TobiSGD 02-26-2013 12:45 PM

Quote:

Originally Posted by lleb (Post 4900221)
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.

lleb 02-26-2013 01:13 PM

thanks. always trying to learn.

Don_Nadie 03-03-2013 12:00 PM

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.


All times are GMT -5. The time now is 07:47 PM.