#!/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.
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].