Combining ls and shuf ... ... and more

Sérgio Araújo - May 15 '22 - - Dev Community

Introduction

Portuguese version

Some commands do not generate any output list, authough dealing with lists from other commands, the shuf command is one of these. If I want to get a random image name from my wallpapers dir I do:

ls -1A ~/img/backgrounds/* | shuf -n 1
Enter fullscreen mode Exit fullscreen mode

The above command could be used to create a variable and be used as other command argument, in this particular case a command that, based on your wallpaper color, will set your terminal colorscheme:

sudo pip3 install pipx
pipx install pywal
Enter fullscreen mode Exit fullscreen mode

The pipx command installs your python modules in a sandbox, avoiding namespaces clashes. After installing pywal you will have a wal command, using it I have created this shell script line:

/home/sergio/.local/bin/wal -i "$(ls -1A ~/img/backgrounds/* | shuf -n 1)"
Enter fullscreen mode Exit fullscreen mode

Change the path and system variables accordingly, after that open up a new terminal and run the following command:

Update!

Watching more videos about pywal I have learned that you do not need to use the shuf command, just give a directory to the wal command and it will choose a random image from there, but the knowledge on using shuf will eventually

wal --preview
Enter fullscreen mode Exit fullscreen mode

This way every time you run the command...

wal -i "path-of-your-wallpapers"
Enter fullscreen mode Exit fullscreen mode

...you will see a preview of your terminal theme.

In the case of my "tiling window manager", the bspwm, I have a shortcut on my sxhkdrc:

# change background
super + b
    /home/sergio/.local/bin/wal -qi ~/img/backgrounds/ 
Enter fullscreen mode Exit fullscreen mode

Also one line on my bspwmrc

/home/sergio/.local/bin/wal -qi ~/img/backgrounds/ 
Enter fullscreen mode Exit fullscreen mode

Finally I have one line on my crontab that runs the same script every five minutes:

# crontab -e
# vim:et:sw=4:ts=4:cms=#%s:
# >/dev/null 2>&1   <- used to preven sending e-mail's
# env | grep -i display  to get your DISPLAY variable
# otherwhise feh will not be abble to change your background
# source: https://superuser.com/a/1122245/45032
DISPLAY=':0'
SHELL=/bin/sh
PATH=/sbin:/bin:/usr/sbin:/usr/bin:/home/sergio/.dotfiles/algorithm/shell/bash/
XDG_PICTURES_DIR=/home/sergio/img
WALLPAPER_DIR=$XDG_PICTURES_DIR/backgrounds/
WAL=/home/sergio/.local/bin/wal
MAILTO=""
*/5    *    *    *    *  $WAL -qi $WALLPAPER_DIR
# */5    *    *    *    *  feh -z --bg-fill /home/sergio/img/backgrounds/*
# */5     *     *     *     *  wal -i "${XDG_PICTURES_DIR}/backgrounds/$(ls ${XDG_PICTURES_DIR}/backgrounds | shuf -n 1)"
#*/30    *    *    *    *  mpg123 /home/sergio/music/drink-water.mp3
Enter fullscreen mode Exit fullscreen mode

Randomization in other apps:

Many utilities have their own flag for randomization, the mpg123 for example has a -Z flag for that.

For those apps that do not have randomization we can also use a shell trick ...

echo $((RANDOM % 10))
Enter fullscreen mode Exit fullscreen mode

... Where 10 is the maximum randomized number possible

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .