1

I am having trouble writing a little program; when I run the following command from my program:

gsettings get org.gnome.desktop.background picture-uri

I get:

'file:///home/thamara/.config/variety/Favorites/wallpaper-2448037.jpg'

But that needs to become:

/home/thamara/.config/variety/Favorites/wallpaper-2448037.jpg

Or the program will not work. Can someone please help me?

The program:

#!/bin/bash
## Blogry for GDM - Blurs your current wallpaper and puts it on your loginscreen

## Some Settings
effect='0x8' # Change this to anything you like http://www.imagemagick.org/Usage/blur/
save='~/Pictures/blur.jpg'

## Step one -  getting the current wallpaper
dir=$(gsettings get org.gnome.desktop.background picture-uri)

## Step two - Blurring the wallpaper
convert $dir -blur $effect -blur $effect -blur $effect $save

## Step three - exit (Since GDM automatically loads the new wallpaper there is no need for seting it.)
exit 0

## Links:
# http://www.imagemagick.org/Usage/blur/
glaasje
  • 13
  • 4

3 Answers3

2

When I run this command on Ubuntu 12.10 I get the following:

$ dir=$(gsettings get org.gnome.desktop.background picture-uri)
$ echo $dir
'file:///usr/share/backgrounds/warty-final-ubuntu.png'

I would just cleanup the value getting stored in $dir like so:

$ dir="${dir/file:\/\//}"
$ echo $dir
'/usr/share/backgrounds/warty-final-ubuntu.png'

This will truncate the file:// from the beginning of the string. You can change this if you're getting something different. In your case:

$ dir="${dir/\/\//}"

Details

The above is using a pattern substitution, ${var/pattern/} which will remove pattern from the variable $var.

Alternatives

@jthill also had a good suggestion of using Bash's "remove matching pattern prefix` notation instead. It's a little trickier to understand, IMO, but works equally as well.

Example

$ dir="\'${dir#\'file://}"

The above is removing the prefix, \'file:// from $dir. It's replacing it with a tick, ', followed by the remainder of $dir without the 'file://.

Bash man page

If you want to read up more on these features of Bash, I'd encourage you to do so. These are features that we're using above.

excerpts from Bash man page

${parameter#word}
${parameter##word}
       Remove matching prefix pattern.  The word is expanded to produce a 
       pattern just as in pathname expansion.  If the pattern matches the 
       beginning of  the  value  of  parameter, then  the  result  of  the  
       expansion is the expanded value of parameter with the shortest 
       matching pattern (the ``#'' case) or the longest matching pattern 
       (the ``##'' case) deleted.  If parameter is @ or *, the pattern 
       removal operation is applied to each positional parameter in turn, 
       and the expansion is the resultant list.  If parameter is  an array 
       variable subscripted with @ or *, the pattern removal operation is 
       applied to each member of the array in turn, and the expansion is the 
       resultant list.

${parameter/pattern/string}
       Pattern substitution.  The pattern is expanded to produce a pattern 
       just as in pathname expansion.  Parameter is expanded and the longest 
       match of pattern against  its  value is replaced with string.  If 
       pattern begins with /, all matches of pattern are replaced with 
       string.  Normally only the first match is replaced.  If pattern 
       begins with #, it must match at the beginning of the expanded value 
       of parameter.  If pattern begins with %, it must match at the end of 
       the expanded value of parameter.  If  string  is  null, matches  of  
       pattern  are  deleted  and the / following pattern may be omitted.  
       If parameter is @ or *, the substitution operation is applied to each 
       positional parameter in turn, and the expansion is the resultant 
       list.  If parameter is an array variable subscripted with @ or *, the 
       substitution operation is applied to each member of  the  array in 
       turn, and the expansion is the resultant list.

Follow-up Question #1

The OP asked the following in comments below.

now i am having the following problem.. unable to open image '/home/thamara/.config/variety/Downloaded/wallbase_type_all_order_random_nsfw_1‌​00_board_1/wallpaper-2249773.jpg''

The issue, if you'll notice, is that there are 2 tick marks at the end of the string. I have no idea why those are there, but if you'd like to get rid of the trailing tick marks you can use this sed command right after the previous substitution I gave you. I couldn't figure out a way to deal with the 2 single tick marks at the end just using Bash's substitution features.

dir=$(echo "$dir" | sed "s/''/'/")

Example

$ echo "$dir"
'/home/thamara/.config/variety/Downloaded/wallbase_type_all_order_random_nsfw_1‌​00_board_1/wallpaper-2249773.jpg''

$ dir=$(echo "$dir" | sed "s/''/'/")
$ echo "$dir"
'/home/thamara/.config/variety/Downloaded/wallbase_type_all_order_random_nsfw_1‌​00_board_1/wallpaper-2249773.jpg'
slm
  • 369,824
  • dir=\'${dir#\'file://} ports everywhere, I think the pattern substitution might not work in #!/bin/sh scripts. – jthill Nov 16 '13 at 03:29
  • @jthill - this was intended to be a Bash only solution given the tag on the Q is Bash. However I just tried both your solution and mine and they both worked in /bin/sh too. – slm Nov 16 '13 at 03:49
  • Yeah, I should have led off with "minor point" or even "very minor point". I didn't know bash won't shut that off even with POSIXLY_CORRECT set. dash rejects it. – jthill Nov 16 '13 at 04:11
  • @jthill - still a good point to highlight. These are some of the lesser known features of Bash, so it's always good to bring attention to them. – slm Nov 16 '13 at 04:15
  • Hunh. I'd have called the pattern substitution the lesser-known one, posix conformance means the prefix/suffix stripping is in every shell, pattern substitution is in the heavier interactive ones. Has debian switched to bash for /bin/sh? Their wiki says it's dash. – jthill Nov 16 '13 at 04:33
  • @jthill - I wasn't implying prefix was lesser known, that both are equally lesser known. 8-). I have no idea, I don't use Debian/Ubuntu, I use Redhat distros. My ancient version of Fedora didn't have the gsettings' so I switched over to an Ubuntu system I have just for this Q. Based on that link you referenced, I would assume it's dash in Squeeze and up. – slm Nov 16 '13 at 04:41
  • @slm :Thank you for your anwser but now i am having the following problem.. :
    convert: unable to open image'/home/thamara/.config/variety/Downloaded/wallbase_type_all_order_random_nsfw_100_board_1/wallpaper-2249773.jpg'': No such file or directory @ error/blob.c/OpenBlob/2643. convert: no decode delegate for this image format '/home/thamara/.config/variety/Downloaded/wallbase_type_all_order_random_nsfw_100_board_1/wallpaper-2249773.jpg'' @ error/constitute.c/ReadImage/552. convert: no images defined/home/thamara/Pictures/blur.jpg' @ error/convert.c/ConvertImageCommand/3145.`
    – glaasje Nov 16 '13 at 18:19
  • 1
    I think that it is because of these things ---> ` <--- – glaasje Nov 16 '13 at 18:21
  • Yes you're output includes too many single tick marks, you need to clean those up so that its only wrapped liked this: '/path/to/file.png'. – slm Nov 16 '13 at 18:22
  • How do i do this?? I have tried dir="${dir/''}" but that doesn't seem to work.. – glaasje Nov 16 '13 at 23:32
  • @glaasje dir=$(gsettings etc.); echo "$dir"; eval dir=$dir; echo $dir; dir="${dir#file://}"; echo "$dir". The eval handles the pre-quoted output. – jthill Nov 17 '13 at 00:59
  • Sorry but it is still the same problem.. 'convert: unable to open image '/home/thamara/.config/variety/Favorites/wallpaper-44803.jpg'': No such file or directory @ error/blob.c/OpenBlob/2643. convert: no decode delegate for this image format'/home/thamara/.config/variety/Favorites/wallpaper-44803.jpg'' @ error/constitute.c/ReadImage/552. convert: no images defined `/home/thamara/Pictures/blur.jpg' @ error/convert.c/ConvertImageCommand/3145.' I have tried it on a different way ( with grep but that didnt work. – glaasje Nov 17 '13 at 15:28
  • @glaasje - What we've provided should be working. We need more info to help. The issue is that there is too many tick marks at the end of the path, so there is likely something that is different than how you're describing the problem. I've tried these commands on my end with your strings and they work just fine. – slm Nov 17 '13 at 16:15
2

The output from the gsettings command complies with the GVariant text syntax. This parameter is a string, printed out in single quotes.

You need to remove the quotes.

dir_uri=$(gsettings get org.gnome.desktop.background picture-uri |
          sed "s/^'//; s/'\$//; s/\\\\\\(.\\)/\\1/")

You then get a URI. If the file name doesn't contain any special characters, it's enough to remove file:// at the beginning (or even file:).

dir=$(gsettings get org.gnome.desktop.background picture-uri |
      sed "s~^'file://~~; s~'\$~~")
  • Sir, You are a genious! Thank you for everything and here is a link to the completed program.. :3 [http://pastebin.com/0fU5tUXt] – glaasje Nov 19 '13 at 14:45
0

If all you want to do is eliminate the extra two // at the start of the file name, and the '' that is oddly showing up at the end of the file name you can use sed:

dir=$(gsettings get org.gnome.desktop.background picture-uri|sed -e 's/\/\/\//\//' -e "s/'//g; s/^/'/; s/$/'/")

This will replace /// with / and delete any erroneously formatted instances or duplicates of '. Finally, it will add a ' at the beginning and end of the line as to properly encapsulate the file name if it has any spaces. This solution is a bit long but it should properly do the job.

I will also echo what slm mentioned and tell you that when I ran:

gsettings get org.gnome.desktop.background picture-uri

I got a result in the format of:

'file:///usr/share/backgrounds/warty-final-ubuntu.png'
John B
  • 606
  • Sorry but this does not fix my problem.. "convert: unable to open image /home/thamara/.config/variety/Favorites/wallpaper-44803.jpg'': No such file or directory @ error/blob.c/OpenBlob/2643. convert: no decode delegate for this image format/home/thamara/.config/variety/Favorites/wallpaper-44803.jpg'' @ error/constitute.c/ReadImage/552. convert: no images defined `/home/thamara/Pictures/blur.jpg' @ error/convert.c/ConvertImageCommand/3145." – glaasje Nov 17 '13 at 15:31
  • @glaasje I have updated my answer to work based on the error you are receiving. – John B Nov 17 '13 at 22:10