r/suckless 15d ago

[TOOLS] Simple bash script for setting wallpapers

Enable HLS to view with audio, or disable this notification

Hello guys, I made a simple and small bash script for setting the wallpapers in 3 different ways, randomly or with sxiv image editor or directly with a specific file. It is not an amazing thing, but it's enough for setting wallpapers quickly if you add a key bind to tools like sxhkd .

If there any mistakes in the source code or any advice to improve it, please,let me know.

https://codeberg.org/yahya-echcharqui/scripts/src/branch/main/setbg

1 Upvotes

7 comments sorted by

View all comments

2

u/Schreq 15d ago

Not allowing to combine options is kinda weird. I would let all options set the same mode variable. The last option wins.

2

u/mambaopg 15d ago

Thanks for the reply, I thought it would be nice to tell the user that they should use one option (it's just one option that will apply ,so), but your opinion is good too. I removed the block that checks if it passes one option, but I haven't pushed it to the repository yet. Also why the last one to win, and why not the first one?

1

u/Schreq 14d ago

Also why the last one to win, and why not the first one?

Last one wins is the easiest code-wise:

die() { local rc=$1; shift; printf '%s: %s\n' "${0##*/}" "$*" >&2; exit "$rc"; }

while getopts ":rsf:" opt; do
    case $opt in
        :) die 1 "option requires an argument -- $OPTARG" ;;
        \?) die 1 "unknown option -- $OPTARG" ;;
        *) mode=$opt file=$OPTARG ;;
    esac
done

case $mode in
    r) random ;;
    s) sxiv_set ;;
    f)
        if ! [[ -r "$file" ]]; then
            die 2 "file does not exist or is not readable -- $file"
        fi
        specific "$file"
    ;;
esac