절름발이를 사용하여 쉘 스크립트 내에서 WAV 파일을 인코딩하려면 어떻게해야합니까?
문제
나는 공백이있는 변수를 통해 아티스트 정보를 설정하려고합니다. 절름발이가 튀어 나옵니다. 어쩌면 나는 배쉬로 지체되고 있습니까?
#!/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=$
다음은 스크립트의 출력입니다.
user@host:~/ogg/noartist/unknown_disc$ ./encode.sh
user@host:~/ogg/noartist/unknown_disc$ lame: excess arg The
lame: excess arg The
절름발이 각 루프 반복에 대해 불평합니다 ... 물론.
스크립트를 루프의 반복 중 하나를 반향하기 위해 스크립트를 변경했는데 이것이 출력됩니다.
lame -b 320 --ta "New Kids On The Block" --tl "The Block" --ty 2008 track_01.wav track_01.mp3
이것은 쉘에서 잘 작동합니다 ... 나는 혼란 스럽습니다. 여기서 내가 뭘 잘못하고 있니? 나는 그것이 내 변수의 공간과 관련이 있다는 것을 알고 있지만, 어떻게 고정 해야할지 잘 모르겠습니다.
해결책
"절름발이"는 기능이어야합니다. 참고 : 같은 디렉토리 "./lame"에서 "Lame"을 실행하여 다른 스크립트를 사용하여 결과를 테스트 할 수있었습니다.
#!/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=$!
절뚝거리는:
#!/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 ===============================================
샘플 출력 (부분) :
===============================================
./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"
===============================================
다른 팁
문제는 줄입니다
lame="lame -b $bitrate --ta \"$artist\" --tl \"$album\" --ty $year"
나중에 $ 절름발이가 두 번 이상 평가되기 때문입니다. 당신은 달릴 수 있습니다
bash -xv ./encode.sh
실행 된 명령과 변수가 대체 된 것을 보려면 ( "bash -xv"를 실행하는 대신 스크립트 내부에서 "set -xv"를 추가 할 수 있음)).
내가 사용한 임시 솔루션을 찾았습니다 ...
약간의 해킹이지만 작업을 수행합니다.
#!/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
제휴하지 않습니다 StackOverflow