2

我正在尝试通过带有空格的变量来设置艺术家信息。跛脚废话。也许我被 bash 智障了?

#!/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 抱怨每次循环迭代......当然。

我更改了脚本以回显循环的迭代之一,这就是输出的内容。

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

这在外壳上工作正常......我很困惑。我在这里做错了什么?我知道这与我的变量中的空格有关,但我不确定如何解决它。

4

3 回答 3

1

问题是线路

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

因为 $lame later 被多次评估。你可以跑

bash -xv ./encode.sh

查看执行的命令和替换的变量(而不是运行“bash -xv”,您可以在脚本中添加“set -xv”)。

于 2009-04-14T21:19:50.850 回答
1

“跛脚”应该是一个功能。注意:我在同一目录“./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"
===============================================
于 2009-04-14T21:50:44.393 回答
0

我找到了一个我使用的临时解决方案......

这有点像黑客,但它可以完成工作:

#!/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
于 2009-04-14T17:19:47.540 回答