Here’s a bash script, it
- gets all files in the current dir, then
- gets all audio files among them (allowing file-names to have whitespace)
- sends the list to the
audacious -p– which should play the list.
The thirt step is where the script fails. Here is the script:
#!/bin/bash
find $1 -name '* *' | while read filename; do
Type=`file -i "$filename" -F "::" | sed 's/.*:: //' | sed 's/\/.*$//'`
if [ $Type=audio ]; then
List="$List '$filename'"
fi
done
audacious2 -p $List &
So the question is: how do i convert
file name 1
file name 2
file name 3
to
'file name 1' 'file name 2' 'file name 3'
in bash?
The main point here is to use
xargsto feed a list of file names to a command, but I hope you will also appreciate how the pattern matching conditional is now a lot more elegant; definitely learn to usecase. (I hope I got the output fromfilecorrect.)Edit Updated to use
find -type f,read -r,tr '\012' '\000',xargs -0. By using a zero byte as a terminator, whitespace and newlines in file names are acceptable toxargs.