I made a bash script to play random music from Music directory

#!/bin/bash

shopt -s nullglob;

# defining rand function which produces unbiased random number
rand() { local max=$((32768 / $1 * $1)) r; while (( (r=$RANDOM) >= max )); do :; done; printf '%s\n' "$(( r % $1 ))"; }

pkill mpv; # killing already playing song

songs=(~/Music/*)
len=${#songs[@]}
random_index="$(rand len)"

if [[ $len == 0 ]]; then
	echo "No songs to play :("
	exit
fi
mpv --no-video "${songs[random_index]}" & disown

Posted in: s/bash

๐Ÿš€ random2934

2024-09-23 ยท 9 months ago

2 Comments โ†“

๐Ÿš€ random2934 [OP] ยท 2024-09-24 at 05:03:

I thought about just having ```mpv -shuffle ~/Music``` however I couldn't key bind this command since I want Ctrl+Shift+M to play a random song.

๐Ÿš€ random2934 [OP] ยท 2024-09-24 at 05:39:

yeah, that would be much simpler :D


Source