Skip to content

Instantly share code, notes, and snippets.

@mansouryaacoubi
Last active August 10, 2018 07:47
Show Gist options
  • Save mansouryaacoubi/919362c7e5a998ecc3ebbaafecbcc06a to your computer and use it in GitHub Desktop.
Save mansouryaacoubi/919362c7e5a998ecc3ebbaafecbcc06a to your computer and use it in GitHub Desktop.
Easy setup for Ubuntu background image (installation using /bin) [recommended]
#!/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 15:53 12.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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment