From b6bcec6d88b9d5a4db9f644ef9de2ff297f2e9e1 Mon Sep 17 00:00:00 2001 From: trotFunky Date: Mon, 20 Nov 2023 11:40:25 +0100 Subject: [PATCH] bootSound: Remove usage of /dev/urandom The bootSound script started to hang on strings /dev/urandom, so replace it with a cleaner random number generation. 24134 could be used for bash, but this would make the script less generic, as it is not available in POSIX. Use awk random() from a suggestion from ShellCheck. Remove the code that was there to control the parsing from /dev/urandom. Other fixes : - Use wc rather than grep to count the files - Use a subshell for proper directory handling --- Perso/bootSound.sh | 22 ++++++++-------------- 1 file changed, 8 insertions(+), 14 deletions(-) diff --git a/Perso/bootSound.sh b/Perso/bootSound.sh index 9c14a2d..7a6b156 100755 --- a/Perso/bootSound.sh +++ b/Perso/bootSound.sh @@ -1,16 +1,10 @@ #! /bin/sh -cd /home/trotfunky/Documents/Sounds/Startup/ -qty=$(ls | grep -c "" ) +( + cd /home/trotfunky/Documents/Sounds/Startup/ || exit 255 + qty=$(ls | wc -l ) + + file_id=$(awk "BEGIN { srand(); print int(rand()*$qty) }" /dev/null) + mplayer -nogui -softvol -ao alsa,pulse "$file_id"_* +) -if [ $qty -lt 10 ]; then - maxBit=1 - diGrep=[[:digit:]] -elif [ $qty -lt 100]; then - maxBit=2 - diGrep=[[:digit:]][[:digit:]] -fi - -mplayer -nogui -softvol -ao alsa,pulse $(expr $(strings /dev/urandom | cut -b 1-$maxBit | grep $diGrep -m 1 ) % $qty )* & -cd - - -exit 0 \ No newline at end of file +exit 0