Cat, shuf ,etc. not working inside of bash script


#1

I have a few scripts that I made on my old instilation of ArchLabs that worked flawlessly, but I recently just wiped the disk and re-installed ArchLabs and updated the paths in the script file to the correct ones, but I’m finding that the cat and shuf command do no work in the script. For example:

#!/bin/bash

find /mnt/c3p1/mov -iregex '.*\.\(mp4\|mpg\|avi\|mpeg\|flv\|wmv\|webm\|mkv\|mov\|m4v\|3gp\|3g2\asf\)$' > /mnt/c3p1/movlibrary.m3u &

find /home/fritz/Documents/c3p1/mov -iregex '.*\.\(mp4\|mpg\|avi\|mpeg\|flv\|wmv\|webm\|mkv\|mov\|m4v\|3gp\|3g2\asf\)$' >> /mnt/c3p1/movlibrary.m3u &

cat movlibrary.m3u | shuf --random-source="/dev/urandom" > movrandomlibrary.m3u &
mpv --profile=c3p1 movrandomlibrary.m3u &

the find command works, and I can follow it after executing the script because it does its job and populates movlibrary.m3u. But after that the script stalls, and the cat command doesn’t work, which causes movrandomlibrary.m3u not to be populated.

I can run cat movlibrary.m3u | shuf --random-source="/dev/urandom" > movrandomlibrary.m3u & in the terminal, and it works just fine, like its supposed to, but when I try to execute this line in a shell script nothing happens, and an error isn’t thrown aside from it not executing the cat command.


#2

Does it work if you don’t background the find commands? (ie remove the & at the end of the lines)


#3

It does work if I do that. That’s strange because I haven’t touched anything in these scripts from my last install except for the paths. Is there a particular reason sending it to the background does this? Thanks for helping me out.

EDIT: Actually it was working when I removed the shuf command, if I add the shuff command back into it after removing the background processes, it reverts back to its old behavior of not working. If I just have something like this:

cat movlibrary.m3u > movrandomlibrary.m3u

then it works, but if I add:

cat movlibrary.m3u | shuf --random-source="/dev/urandom" > movrandomlibrary.m3u

then it doesn’t work in the script, but if I try it in the terminal, it works.

EDIT EDIT EDIT? :

Okay, so removing the & background process from the find command works, but in my tinkering to get the script working I also switched over to /dev/random. So when I took the background process off of the find commands, and executed the script it launched with the available entropy, and when I went to relaunch the script a few moments later it didn’t launch because I was out of entropy because I switched over to /dev/random.

TL;DR – So I switched back to /dev/urandom, I took the background process off the find commands, and the script works. Thanks malm for pointing me in the right direction with the background processes.


#4

If you background any processes and need to wait for their completion before continuing you can use the built in wait before the step requiring it.