Nieuws:

Welkom, Gast. Alsjeblieft inloggen of registreren.
Heb je de activerings-mail niet ontvangen?

Auteur Topic: Fout in Bash script dat playlists moet aanmaken.  (gelezen 2497 keer)

Offline bart85

  • Lid
Fout in Bash script dat playlists moet aanmaken.
« Gepost op: 2017/06/23, 17:11:38 »
Hieronder zit toch nog een fout in. Het is voor het maken van afspeellijsten en deze verplaatsen naar een andere directory. Iedere directory word met 'find -exec' mkplaylist aangeroepen. De laatste subdirectory moet $album zijn en de een na laatste $artist. Dit bepaald de naam van de $playlist. $link word de relatieve directory met de bestandsnaam die aan $playlist word toegevoegd.
De fout zit hem in het maken van $album en $artist
#!/bin/bash

musicdir="/home/bart/MUSIC/music.bak/NL_ENG"
playlistdir="$musicdir/playlists"
mkdir -p "$playlistdir"

#usage of relativedir:
#relativedir "/home/bart/MUSIC/music.bak/playlists" "/home/bart/MUSIC/music.bak/NL_ENG/Ilse de Lange/Clean Up (2003)"
function getrelativedir(){
linkdir="$1"
targetdir="$2"
real=$(realpath --relative-to="$linkdir" "$targetdir")
echo $real
}

function mkplaylist(){
if ["$dir" != "$musicdir"]
then
dir="$1"
relativedir="$(getrelativedir "$dir" "$playlistdir")"
pushd "$dir"
album="$(basename "$dir")"
artist="$(basename $(dirname "$dir"))"
playlist="$artist-$album.m3u"

[ -e $playlist ] && rm $playlist
for j in *.{mp3,ogg}
do
link="$relativedir/$j"
echo "$link" >> $playlist
done
[ -e "$playlist" ] && mv $playlist "$playlistdir"
popd
dirs -c
fi
}
export -f mkplaylist

find "$musicdir" -type d -exec bash -c 'mkplaylist "{}"' \;
}
« Laatst bewerkt op: 2017/06/23, 20:55:16 door bartveurink »
Je leert maar mooi over weg gaan met de commandline.
Linus: "I'm happy with the people who are wandering around looking at the stars but I am looking at the ground and I want to fix the pothole before I fall in."
I look to the clouds behind me and see the thunder coming.

Offline bart85

  • Lid
Re: Fout in Bash script dat playlists moet aanmaken.
« Reactie #1 Gepost op: 2017/06/30, 17:45:22 »
Eerste fout vanwege gebruik van export -f is toevoegen van het volgende:
export -f getrelativedirVervolgens werkt realpath nog steeds niet:
realpath: '': No such file or directory

Onderstaande errors waarbij regelnummers niet kloppen, vanwege het gebruik van `export -f`
bash: line 7: [: too many arguments
bash: line 11: $playlist: ambiguous redirect
Kan iemand uitleggen wat de errors betekenen en wat er aan te doen is?
Je leert maar mooi over weg gaan met de commandline.
Linus: "I'm happy with the people who are wandering around looking at the stars but I am looking at the ground and I want to fix the pothole before I fall in."
I look to the clouds behind me and see the thunder coming.

Offline bart85

  • Lid
Re: Fout in Bash script dat playlists moet aanmaken.
« Reactie #2 Gepost op: 2017/06/30, 18:21:02 »
$playlist: ambiguous redirectDit is verholpen door quotes te gebruiken:
echo "$link" >> "$playlist"
Je leert maar mooi over weg gaan met de commandline.
Linus: "I'm happy with the people who are wandering around looking at the stars but I am looking at the ground and I want to fix the pothole before I fall in."
I look to the clouds behind me and see the thunder coming.

Offline bart85

  • Lid
Re: Fout in Bash script dat playlists moet aanmaken.
« Reactie #3 Gepost op: 2017/06/30, 18:23:38 »
Het probleem met realpath is opgelost door export van de benodigde variabelen te gebruiken.
Nu houd ik de volgende fout over:
bash: line 0: [: missing `]'
Je leert maar mooi over weg gaan met de commandline.
Linus: "I'm happy with the people who are wandering around looking at the stars but I am looking at the ground and I want to fix the pothole before I fall in."
I look to the clouds behind me and see the thunder coming.

Offline bart85

  • Lid
Re: Fout in Bash script dat playlists moet aanmaken.
« Reactie #4 Gepost op: 2017/06/30, 18:27:24 »
Hier moesten spaties bij het if statement ingevoegd worden en is opgelost. Nu krijg ik onderstaande error:
bash: line 7: [: too many arguments
Je leert maar mooi over weg gaan met de commandline.
Linus: "I'm happy with the people who are wandering around looking at the stars but I am looking at the ground and I want to fix the pothole before I fall in."
I look to the clouds behind me and see the thunder coming.

Offline bart85

  • Lid
Re: Fout in Bash script dat playlists moet aanmaken.
« Reactie #5 Gepost op: 2017/07/30, 16:06:54 »
Na veel gedoe zijn de problemen opgelost. Het oplossen ging beter door de functie in een apart script te zetten en die met find aan te roepen. Toen de fouten eruit waren, heb ik alles in een script teruggezet en de functie weer aangemaakt. Zie onderstaande script:
#!/bin/bash

export musicdir="/home/bart/MUSIC/music.bak/NL_ENG"
export playlistdir="$musicdir/playlists"
mkdir -p "$playlistdir"

function mkplaylist(){
shopt -s nullglob
dir="$1"
echo "dir=$dir"
pushd "$dir"
album="$(basename "$dir")"
artist="$(basename "$(dirname "$dir")")"
playlist="$artist-$album.m3u"
echo "album=$album"
echo "artist=$artist"
echo "playlist=$playlist"
[ -e $playlist ] && rm $playlist
for i in *.{mp3,ogg}
do
file="$dir/$i"
link="$(realpath --relative-to="$playlistdir" "$file")"
echo "file=$file"
echo "link=$link"
echo "$link" >> "$playlist"
done
[ -e "$playlist" ] && mv "$playlist" "$playlistdir"
popd
}

export -f mkplaylist

find -L "$musicdir" -type d -exec bash -c 'mkplaylist "{}"' \;
« Laatst bewerkt op: 2017/07/30, 16:20:09 door bartveurink »
Je leert maar mooi over weg gaan met de commandline.
Linus: "I'm happy with the people who are wandering around looking at the stars but I am looking at the ground and I want to fix the pothole before I fall in."
I look to the clouds behind me and see the thunder coming.