It comes down to being able to save the dynamic output
(I'll explain) of terminal into a text file, but here's what I'm ultimately seeking.
I created a custom command for terminal called playRandom
, what it does is that it plays random songs forever. The bash file I created for this:
#!/bin/bash
find ./ -type f | sort -R | xargs -I + play +
Note: The play
command is from SoX software.
Now the output looks something like this:
As you can see the output changes dynamically
, so I cannot use >>
to save the output.
I want to be able to save 'names of songs that are played' into a text file.
How can I achieve this goal? Thanks in advance.
Best Answer
Saving filenames that are played currently
Since the
play
command terminates after playing a single file, what we can do is instead of usingxargs
and givingplay
a batch of files, we'll take out each file, one at a time, and echo it to file, and play the file afterwards. The edited script would look like below. Notice that here are added additional options andIFS= read -d'' -r
command to deal with filenames in safe manner.The advantage of this approach is that filenames will go into playlist.txt as they are played, which allows us to track output of the script in real time with something like
tail -F playlist.txt
.NOTE: to avoid
playlist.txt
being listed infind
's output changefind
command like so:Additionally if we want to ensure that only
.mp3
files are listed we can do this:Saving list of found files to be played
If our goal is to safe the file list before it is played, there's not a lot of science to it - the find-sort pipeline can be written to file first, and that file can then be fed to
play
either viaxargs
or again viawhile IFS= read -r ; do ... done
structure