Reputation: 333
Trying to write a script that use vlc to create a playlist.
#!/bin/bash
filename=/media/*/*.mp3
while [ "$1" != "" ]; do
case $1 in
-f | --filepath ) shift
filename=$1
;;
-h | --help ) usage
exit
;;
* ) usage
exit 1
esac
shift
done
#echo $filename
vlc $filename --novideo --quiet
This script is working but it only finds mp3 files in the root of any usb device. So i want to change the filename variable. This code gives similar results but it lists evething.
filename=$(find /media/* -name *.mp3 -print)
filename=$(tr '\n' ' ' <<<$filename)
Now the problem is that i can't pass it as an argument. I tried:
vlc $filename --novideo --quiet
or
vlc $*filename --novideo --quiet
or
vlc "$filename" --novideo --quiet
nothing worked. Any suggestions?
UPDATE:
Guys the problem I want help with is how to make vlc accept the filename variable as argument or arguments of files to use in the playlist. filename contains
/media/MULTIBOOT/Linkin Park - In The End.mp3 /media/MULTIBOOT/Man with a
Mission ft. Takuma - Database.mp3 /media/MULTIBOOT/Sick Puppies - You're
Going Down.mp3 /media/MULTIBOOT/Skillet - Rise.mp3 /media/MULTIBOOT/Song
Riders - Be.mp3 /media/MULTIBOOT/30 Seconds to Mars - This is War.mp3
/media/MULTIBOOT/Fade - One Reason.mp3
Now this is a string how to use it file path arguments?
Upvotes: 1
Views: 118
Reputation: 247052
I would use bash's recursive globbing and arrays:
#!/bin/bash
shopt -s globstar nullglob
files=()
while [[ $1 ]]; do
case $1 in
-f | --filepath ) shift
files+=("$1")
;;
-h | --help ) usage
exit
;;
* ) usage
exit 1
esac
shift
done
if [[ ${#files[@]} -eq 0 ]]; then
files=( /media/**/*.mp3 )
if [[ ${#files[@]} -eq 0 ]]; then
echo "no mp3 files found"
exit 1
fi
fi
#printf "%s\n" "${files[@]}"
vlc "${files[@]}" --novideo --quiet
With this code, you can specify -f filename
multiple times to play a few songs.
Upvotes: 3
Reputation: 46876
A better way to handle options in your script might be to use getopts
, if you don't mind losing the option of long options. For example:
#!/usr/bin/env bash
while getopts vqt opt; do
case "$opt" in
f) filename=($OPTARG) ;;
h) usage; exit 0 ;;
*) usage; exit 1 ;;
esac
done
shift $((OPTIND - 1))
filename=($(find /media/ -name \*.mp3 -type f))
vlc --novideo --quiet "${filename[@]}"
I don't know this usage of VLC, but the effect of this script is to create a command line with all the files found by the find
command, which were stored in array called $filename
.
An advantage of handling things in an array is that it lends itself to use in for
loops.
for thisfile in "${filename[@]}"; do
vlc "$thisfile" # with options to convert just one file
done
NOTE that since you're using bash, you may not need to use find
at all.
shopt -s globstar
filelist=(/media/**/*.mp3)
Check man bash
for discussion of globstar
.
Upvotes: 1
Reputation: 31334
rather than storing all filenames in a variable, you can tell find
to call an application with all the files. this will prevent problems with whitespace, newlines and the like:
find /media -name '*.mp3' -exec vlc --novideo --quiet \{\} \+
Upvotes: 1
Reputation: 782105
You need to quote *.mp3
. Otherwise it will be expanded in the current directory.
filename=$(find /media/* -name '*.mp3' -print)
You also don't need to remove the newlines. When you use the variable without quoting it, all whitespace, including newlines, will be converted to word delimiters.
Upvotes: 1