2009-04-14 4 views
2

J'essaie de définir des informations d'artiste via des variables avec des espaces en eux. Lame craps out. Peut-être que je suis retardé avec bash?Comment utiliser lame pour encoder des fichiers wav dans un script shell?

#!/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=$ 

Voici la sortie du script. Lame se plaint pour chaque itération de boucle ... bien sur.

J'ai changé le script pour renvoyer l'une des itérations de la boucle et c'est ce qui est sorti.

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

Cela fonctionne très bien sur la coque ... Je suis confus. Qu'est-ce que je fais mal ici? Je sais que cela a à voir avec les espaces dans mes variables, mais je ne suis pas sûr de savoir comment y remédier.

Répondre

1

"lame" devrait être une fonction. Note: J'ai couru "lame" dans le même répertoire, "./lame", donc je pourrais utiliser un autre script pour tester les résultats.

#!/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 =============================================== 

sortie de l'échantillon (partiel):

=============================================== 
./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

je trouve une solution temporaire que je ...

Il est un peu un hack, mais il fait le travail:

#!/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

Le problème est la ligne

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

car $ lame plus tard est évalué plus d'une fois. Vous pouvez exécuter

bash -xv ./encode.sh 

pour voir les commandes exécutées et les variables substituées (au lieu de courir « bash -xv » vous pouvez ajouter « set -xv » à l'intérieur du script).

Questions connexes