Bash Script Random Backgrounds (or Wallpapers)

About writing shell scripts and making the most of your shell
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Locked
edcompsci
Level 2
Level 2
Posts: 69
Joined: Fri Dec 21, 2012 5:25 pm

Bash Script Random Backgrounds (or Wallpapers)

Post by edcompsci »

I just wanted to share my script.

Code: Select all

#!/bin/bash
# before running this run command
# touch /path/to/your/backgrounds/default.png
#set your background by right-clicking on
# your desktop and choosing /path/to/your/backgrounds/default.png

while true
do

sleep 15

dir='/path/to/your/backgrounds'
file=`/bin/ls -1 "$dir" | sort --random-sort | head -1`
path=`readlink --canonicalize "$dir/$file"` # Converts to full path
chmod 777 "/path/to/your/backgrounds/default.png"
rm "/path/to/your/backgrounds/default.png"
dd if="$path" of="/path/to/your/backgrounds/default.png"
echo "The randomly-selected file is: $path"

done
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 2 times in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
User avatar
Pilosopong Tasyo
Level 6
Level 6
Posts: 1432
Joined: Mon Jun 22, 2009 3:26 am
Location: Philippines

Re: Bash Script Random Backgrounds (or Wallpapers)

Post by Pilosopong Tasyo »

Some observations / comments / [unsolicited] advice:
  • Use code tags instead of quote tags when it comes to program code and terminal output. Kindly read the guide on how to post on the forums. Indenting program code also improves readability.
  • You have declared a variable dir='/path/to/your/backgrounds' and only used it once. Use the same $dir variable in the chmod, rm and dd lines in lieu of hard-coding /path/to/your/backgrounds in those same lines. Also, the declaration can be moved above the while loop. It's a static variable anyway.
  • Skip the readlink command. Using path="$dir/$file" is canonical enough.
  • dd command instead of cp? dd is overkill in this case, (and inappropriate, IMHO). cp is sufficient.
  • After changing the permissions of the default.png file, you delete it, then create (copy) a new default.png file from a random background file? The rm command is kind of pointless and can be removed. cp can overwrite existing files without asking.
  • Pitfall! you have a logic bug. default.png and other wallpapers are located in the same directory. If the next randomly-selected wallpaper is default.png, your script will delete default.png before it gets replaced by... well, itself (which now doesn't exist). Your script doesn't have a way to catch this logic error.
o Give a man a fish and he will eat for a day. Teach him how to fish and he will eat for a lifetime!
o If an issue has been fixed, please edit your first post and add the word [SOLVED].
edcompsci
Level 2
Level 2
Posts: 69
Joined: Fri Dec 21, 2012 5:25 pm

Re: Bash Script Random Backgrounds (or Wallpapers)

Post by edcompsci »

I know about code tags, I must have just clicked the wrong button, sorry about that.

The program runs beautifully as it is, but I welcome any ways to improve it.


Thanks for your insight on my code, I will take a deeper look at it when I am home.
Locked

Return to “Scripts & Bash”