3

I am trying to create a script, which will change the wallpaper automatically when run.

#!/bin/bash

cd ~/
rm -r ~/.wallpaper
mkdir .wallpaper
cd ~/.wallpaper

wget https://source.unsplash.com/random/1920x1080
USER=$(whoami)
PATH="file:///home/$USER/.wallpaper/1920x1080"
echo $PATH
gsettings set org.gnome.desktop.background picture-uri "$PATH"

But when i do ./change_wallpaper.sh I get the echo correctly, but then

./change_wallpaper.sh: line 12: gsettings: command not found

However, when I run the same command from terminal, it executes fine and wallpaper is getting changed.

When I run whereis gsettings it tells

gsettings: /usr/bin/gsettings /usr/share/man/man1/gsettings.1.gz

Why is it showing gsettings: command not found when I execute from script?

4

Because you change the PATH in your script. This reserved variable is used to locate executable files. Use another variable.

Same with USER: it is reserved as well and already contains the current user, i.e. you do not need to set USER=$(whoami).

In general, when creating variables in shell scripts it is a good idea to use lowercase names. Usually, predefined variables (like HOME, USER, PATH) are all UPPERCASE and a simple way to avoid overwriting them is to use lowercase names in own scripts. Or use some prefix, e.g. MY_PATH, MY_USER etc.

  • That did solve my problem. I was already aware of the PATH Environment variable. But, was not aware changing it in script, will make my commands to fail. Thanks – Saran Sankaran Apr 8 '18 at 9:26
4

Your script won't work for everyone. The variable you set for home for your user will be incorrect for people who have their HOME location in a different place from /home/user. For instance, my home location for my personal space is /home/user/l/j/ljames.

Instead of setting the path to be "file:///home/$USER/.wallpaper/1920x1080" you should more correctly change it to "file:///$HOME/.wallpaper/1920x1080". The variable $HOME is already expanded to the full home space of the user.

Your script will work if with these changes:

#!/bin/bash

cd ~/
rm -r ~/.wallpaper
mkdir .wallpaper
cd ~/.wallpaper

wget https://source.unsplash.com/random/1920x1080
# USER=$(whoami) This line isn't neccesary.)    
path="file:///$HOME/.wallpaper/1920x1080"
echo $path
gsettings set org.gnome.desktop.background picture-uri "$path"

A more efficient example is:

#!/bin/bash

[ ! -d ~/.wallpaper ] && mkdir ~/.wallpaper
cd ~/.wallpaper
wget -O 1920x1080 https://source.unsplash.com/random/
path="file:///$HOME/.wallpaper/1920x1080"
rm ~/.cache/wallpaper/*
gsettings set org.gnome.desktop.background picture-uri "$path"

The explaining of the lines are:

Line #1:  Create wallpaper directory if it doesn't exist.
Line #2:  Move to the folder.
Line #3:  Overwrite the current file with the new wallpaper.
Line #4:  Set the pathname variable.
Line #5:  Removed the Wallpaper cache for image change detection.
Line #6:  Active the new wallpaper.
  • I was not aware, there is a HOME variable and we can have home directory after many directories from /home/. In the first line where we are creating a directory there is only one [ and two ] why is that syntax like this? – Saran Sankaran Apr 8 '18 at 10:29
  • 1
    @PerlDuck That's correct... I fixed it. – L. D. James Apr 8 '18 at 10:36
  • 1
    @SaranSankaran An ordinary user's home directory usually (and by convention) is right below /home but it can be anywhere else. Especially technical users often have their home directory elsewhere. For example the user www-data (created and used by apache) has /var/www as their home directory. – PerlDuck Apr 8 '18 at 11:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.