Skip to content

Instantly share code, notes, and snippets.

@mansouryaacoubi
Last active June 12, 2017 12:36
Show Gist options
  • Save mansouryaacoubi/aee3556c9fda7b726f4ca26a5717523c to your computer and use it in GitHub Desktop.
Save mansouryaacoubi/aee3556c9fda7b726f4ca26a5717523c to your computer and use it in GitHub Desktop.
Easy setup for Ubuntu background image

Simple Background Image Changer Script for Linux

This script uses gsettings to set the gnome.background of the current user.

setbg.sh vers. 1.5

You can use it as a standalone script to set the background image or you can even add this script to your .bash_aliases so that you can use the simple setbg-alias for the script.

setbg.sh vers. 2.3 [recommended]

You can use it as a standalone script to set the background image or you can even install this script to your binary folder so that you can use the simple setbg-command for the script.

#!/bin/bash
# @author Mansour Yaacoubi
# @filename setbg.sh
# @version 2.3
# @task Sets background in Ubuntu and some other Linux versions
# @feature When this script runs as superuser,
# the command 'setbg' can be installed into /bin directory
# @timestamp 12:23 07.06.2017
# @usage ./setbg.sh wallpaper.jpg
# su $USER -c setbg.sh wallpaper.jpg
# setbg wallpaper.jpg (after script was installed)
boldcmd="\033[1msetbg\033[0m"
bin=/bin/setbg
TMP=/tmp
tmpscript=$TMP/$(basename $0)
# Check whether help option is chosen
if [ "$1" == "--help" ] || [ "$1" == "-h" ]; then
echo "Usage: setbg [OPTION] [FILE]"
if [ ! -f $bin ]; then echo "Example: sudo $0 -i"; fi;
echo "Example: setbg wallpaper.jpg"
echo "Sets background of current user. Expects wallpaper-image as first argument."
if [ ! -f $bin ]; then echo "Install-option needs root-privileges."; fi;
echo ""
echo "Mandatory arguments to long options are mandatory for short options too."
echo -e "\\t-h, --help\\t\\tprints this help text"
if [ ! -f $bin ]; then echo -e "\\t-i, --install\\t\\tinstalls this script; needs to run as sudo"; fi;
exit
# Check whether install option is chosen
elif [ "$1" == "--install" ] || [ "$1" == "-i" ]; then
if [ -f $bin ]; then
echo "Command has already been installed."
exit
fi
if [ "$UID" -ne 0 ]; then
echo -e "To install this script ($boldcmd), please run as superuser."
echo -e "To install, run script like this:\\n"
echo "sudo $0 $1"
exit
fi
echo -e "Command $boldcmd will be installed..."
# Check whether argument is empty or file does not exists
elif [ -z "$1" ] || [ ! -f "$1" ] && [ "$1" != "install" ]; then
echo "Please provide a valid image-file"
exit
# Set background or warn to use normal user
else
if [ "$UID" -eq 0 ]; then
echo "Please don't run as root to set background."
echo -e "Run script like this:\\n"
if [ -f $bin ]; then echo "$(basename $0) $1"; exit; fi;
echo "$0 $1"
exit
else
# GSettings cannot run with 'sudo' but with 'su $USER -c'
gsettings set org.gnome.desktop.background picture-uri file://$(readlink -f $1)
exit
fi
fi
############################ SUPERUSER AREA ############################
# Everything below this text will be executed with elevated privileges #
########################################################################
cp $0 $TMP
chmod +rx $tmpscript
cp $tmpscript $bin
if [ $? -eq 0 ]; then
echo -e "Command $boldcmd has been installed."
rm $tmpscript
else
echo -e "To install this script ($boldcmd), please run as superuser.\\nTo install, run script like this:\\n\\nsudo $0 $1"
fi
#!/bin/bash
# @author Mansour Yaacoubi
# @filename setbg.sh
# @version 1.5
# @task Sets background in Ubuntu and some other Linux versions
# @feature When this script runs as superuser,
# the command 'setbg' can be added to your aliases
# @timestamp 12:23 07.06.2017
# @usage ./setbg.sh wallpaper.jpg
# su $USER -c setbg.sh wallpaper.jpg
# setbg wallpaper.jpg (after script was added to .bash_aliases)
boldcmd="\033[1msetbg\033[0m"
# Check whether help option is chosen
if [ "$1" == "--help" ] || [ "$1" == "-h" ]; then
echo "Usage: setbg [OPTION] [FILE]"
echo "Example: sudo $0 -i"
echo "Example: setbg wallpaper.jpg"
echo -e "Sets background of current user. Expects install-option or wallpaper-image as first argument. Install-option needs root-privileges to edit .bash_aliases file.\\n"
echo "Mandatory arguments to long options are mandatory for short options too."
echo -e "\\t-h, --help\\t\\tprints this help text"
echo -e "\\t-i, --install\\t\\tinstalls this script; needs to run as sudo"
exit
# Check whether install option is chosen
elif [ "$1" == "--install" ] || [ "$1" == "-i" ]; then
if [ "$UID" -ne 0 ]; then
echo -e "To add this script to your commands ($boldcmd), please run as superuser."
echo -e "To install, run script like this:\\n"
echo "sudo $0 $1"
exit
fi
echo -e "Command $boldcmd will be added to .bash_aliases..."
# Check whether argument is empty or file does not exists
elif [ -z "$1" ] || [ ! -f "$1" ] && [ "$1" != "install" ]; then
echo "Please provide a valid image-file"
exit
# Set background or warn to use normal user
else
if [ "$UID" -eq 0 ]; then
echo "Please don't run as root to set background."
echo -e "Run script like this:\\n"
echo "$0 $1"
exit
else
# GSettings cannot run with 'sudo' but with 'su $USER -c'
gsettings set org.gnome.desktop.background picture-uri file://$(readlink -f $1)
exit
fi
fi
echo "alias setbg='$(readlink -f $0)'">>$HOME/.bash_aliases; if [ $? -eq 0 ]; then cp $0 $0.tmp; sed '$ d' $0.tmp > $0; rm -f $0.tmp; echo "echo \"Command has already been installed.\"" >> $0; echo -e "Command $boldcmd has been added.\n\nPlease restart terminal."; else echo -e "To add this script to your commands ($boldcmd), please run as superuser.\\nTo install, run script like this:\\n\\nsudo $0 $1"; fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment