2

I wrote a small Bash script so that the image in the folder becomes the wallpaper:

for f in *.jpg
do 
    gsettings set org.gnome.desktop.background picture-uri file:$f

The error is:

./wall.sh: line 6: syntax error: unexpected end of file

Is my code wrong? And is there a way to set wallpapers from the Internet using a Bash script?

0

4 Answers 4

7

You need to end it with a "done", like:

for f in *.jpg
do 
    gsettings set org.gnome.desktop.background picture-uri "file:$f"
done

But I think the best way to do it is like this:

for f in /path/to/dir/*.jpg
do
    gsettings set org.gnome.desktop.background picture-uri "file://$f"
done

Okay, I edited the second code. Try with this.

If you want to get random wallpapers from the Internet, you can execute this code:

#!/bin/bash
while true
do 
    img=http://www.wallpaperup.com/wallpapers/download/$RANDOM/1920/1080
    wget -O /tmp/1080.jpg $img
    gsettings set org.gnome.desktop.background picture-uri "file:///tmp/1080.jpg"
    sleep 60
done

This script will change the wallpaper every 60 seconds.

6
  • 1
    I didn't downvote, but I expect the downvotes are coming for the same reason this comment pointed out: askubuntu.com/questions/567474/…
    – Seth
    Dec 31, 2014 at 19:38
  • 1
    There is absolutely no advantage to call ls here: the wildcards do the right thing, but ls breaks things. Furthermore, with the command substitution inside the double quotes, the only case when this will work is if there is exactly one matching file. Dec 31, 2014 at 19:40
  • 1
    The first part is right, but there are so many problems with the second. The parentheses will make it run only a single time with f as all matching files separated by spaces. Don't use ls. Better to use $(...) than ``` ... ```.
    – Kevin
    Dec 31, 2014 at 19:43
  • Well, thanks to telling me this, I didn't know it, I removed it right now. thx.
    – blkpws
    Dec 31, 2014 at 19:43
  • ca =n you please explain why is there two back slashes (file:///tmp/1080.jpg)? I am new to bash scripting and a detailed answer would really help..
    – Bharat
    Jan 1, 2015 at 15:21
5

In bash you end for blocks with a done, like so:

for f in *.jpg
do 
    gsettings set org.gnome.desktop.background picture-uri "file:$f"
done  

Also, put double quotes around $f, in case some file names contain spaces.

Note that I think what you are trying to achieve could be done easier with find:

find $imageDir -maxdepth 1 -type f -name "*.jpg" -exec gsettings set org.gnome.desktop.background picture-uri file:{} \;  

Replace $imageDir with the path to the folder you want to search. Or, if you want to run the command in the local directory you can do it like this:

find -maxdepth 1 -type f -name "*.jpg" -exec gsettings set org.gnome.desktop.background picture-uri file:$PWD/{} \;   

Thanks to Gilles for reminding me about $PWD.

4

Add the last line: done.

Also, put double quotes around $f, in case some file names contain spaces.

Thus, the whole script becomes:

for f in *.jpg
do 
    gsettings set org.gnome.desktop.background picture-uri "file:$f"
done
4

First, basic bash syntax: a for loop ends with the reserved word done.

for f in *.jpg
do 
    gsettings set org.gnome.desktop.background picture-uri file:$f
done

Furthermore $f is a relative path — it's a file name in the current directory. But as feyray remarked that file name would not be meaningful out of context. You need to provide an absolute path. You can do that easily with the PWD variable, which always contains the path to the current directory. Note that file:// URLs require two slashes for the :// indicator plus a slash for the root directory that starts the path, which $PWD includes.

for f in *.jpg
do 
    gsettings set org.gnome.desktop.background picture-uri file://$PWD/$f
done

That's not all. If any file name contains a space, your script won't work. To avoid this pitfall, put double quotes around variable substitutions (always put double quotes around variable substitutions).

for f in *.jpg
do 
    gsettings set org.gnome.desktop.background picture-uri "file://$PWD/$f"
done
1
  • Actually using the two forward slashes on file: isn't strictly needed, gsettings will accept it just fine without, although perhaps it is better form.
    – Seth
    Dec 31, 2014 at 20:14

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .