2009-04-14 2 views
2

Ich versuche, Künstlerinformationen über Variablen mit Leerzeichen in ihnen zu setzen. Lame scheißt aus. Vielleicht bin ich mit Bash zurückgeblieben?Wie kann ich lame verwenden, um WAV-Dateien in einem Shell-Skript zu codieren?

#!/bin/bash 
year=2008; 
artist="New Kids On The Block"; 
album="The Block"; 
bitrate=320; 
lame="lame -b $bitrate --ta \"$artist\" --tl \"$album\" --ty $year" 

function first_half 
{ 
    for ((i=1;i<10;i++)); do 
     $lame "track_0$i.wav" "track_0$i.mp3"; 
    done; 
} 

function second_half 
{ 
    for ((x=10;x<18;x++)); do 
     echo $lame "track_$x.wav" "track_$x.mp3"; 
    done; 
} 

first_half & 
first_pid=$! 

#second_half & 
#second_pid=$ 

Hier ist die Ausgabe des Skripts.

[email protected]:~/ogg/noartist/unknown_disc$ ./encode.sh 
[email protected]:~/ogg/noartist/unknown_disc$ lame: excess arg The 
lame: excess arg The 

Lame beschwert sich für jede Schleife Iteration ... natürlich.

Ich änderte das Skript, um eine der Wiederholungen der Schleife auszugeben, und dies wird ausgegeben.

lame -b 320 --ta "New Kids On The Block" --tl "The Block" --ty 2008 track_01.wav track_01.mp3 

Dies funktioniert Linie in Ordnung auf der Shell ... Ich bin verwirrt. Was mache ich hier falsch? Ich weiß, dass es mit den Leerzeichen in meinen Variablen zu tun hat, aber ich bin mir nicht sicher, wie ich es beheben soll.

Antwort

1

"lame" sollte eine Funktion sein. Hinweis: Ich habe "lame" im selben Verzeichnis "./lame" ausgeführt, sodass ich ein anderes Skript zum Testen der Ergebnisse verwenden konnte.

#!/bin/bash 
year=2008 
artist="New Kids On The Block" 
album="The Block" 
bitrate=320 

function lame() 
{ 
#local bitrate=$1 
#local artist=$2 
#local album=$3 
#local year=$4 
local in=$1 
local out=$2 
./lame -b "$bitrate" --ta "$artist" --tl "$album" --ty "$year" "$in" "$out" 
} 

function first_half 
{ 
    for ((i=1;i<10;i++)); do 
     lame "track_0$i.wav" "track_0$i.mp3" 
    done 
} 

first_half & 
first_pid=$! 

lame:

#!/bin/bash 

echo =============================================== 
echo $0 $* 
echo "0 ==> \"$0\"" 

CNT=1 
while true; do 
    echo -n "$CNT " 
    [ $CNT -lt 10 ] && echo -n " " 
    echo "==> \"$1\"" 
    CNT=$(($CNT + 1)) 
    shift 
    [ -z "$1" ] && break 
done 

echo =============================================== 

Beispielausgabe (teilweise):

=============================================== 
./lame -b 320 --ta New Kids On The Block --tl The Block --ty 2008 track_01.wav track_01.mp3 
0 ==> "./lame" 
1 ==> "-b" 
2 ==> "320" 
3 ==> "--ta" 
4 ==> "New Kids On The Block" 
5 ==> "--tl" 
6 ==> "The Block" 
7 ==> "--ty" 
8 ==> "2008" 
9 ==> "track_01.wav" 
10 ==> "track_01.mp3" 
=============================================== 
=============================================== 
./lame -b 320 --ta New Kids On The Block --tl The Block --ty 2008 track_02.wav track_02.mp3 
0 ==> "./lame" 
1 ==> "-b" 
2 ==> "320" 
3 ==> "--ta" 
4 ==> "New Kids On The Block" 
5 ==> "--tl" 
6 ==> "The Block" 
7 ==> "--ty" 
8 ==> "2008" 
9 ==> "track_02.wav" 
10 ==> "track_02.mp3" 
=============================================== 
0

fand ich eine vorübergehende Lösung, die ich verwendet ...

Es ist ein bisschen wie ein Hack, aber es macht den Job:

#!/bin/bash 
year="2008"; 
artist="\"New Kids On The Block\""; 
album="\"The Block\""; 
bitrate=320; 
genre="Pop"; 
lame="lame -b $bitrate --ta $artist --tl $album --ty $year --tg $genre" 

function first_half 
{ 
    echo "Encoding first half..."; 
    for ((i=1;i<10;i++)); do 
     echo $lame "track_0$i.wav" "track_0$i.mp3" > run1.sh; 
     bash run1.sh >/dev/null 2>/dev/null; 
    done; 
    rm -f run1.sh; 
} 

function second_half 
{ 
    echo "Encoding second half too..."; 
    for ((x=10;x<18;x++)); do 
     echo $lame "track_$x.wav" "track_$x.mp3" >run2.sh; 
     bash run2.sh >/dev/null 2>/dev/null; 
    done; 
    rm -f run2.sh; 
} 

first_half & 
echo $! > first_half.pid 

second_half 
echo $! > second_half.pid 

echo "Done!"; 
rm *.pid -f 
1

Das Problem ist die Linie

lame="lame -b $bitrate --ta \"$artist\" --tl \"$album\" --ty $year" 

weil $ lame später mehr als einmal ausgewertet wird. Sie können laufen

bash -xv ./encode.sh 

Befehle zu sehen, ausgeführt und substituierten Variablen (anstelle von laufenden „bash -xv“ Sie „gesetzt -xv“ im Skript hinzufügen).

Verwandte Themen