2015-08-26 35 views
0

我必须为变量赋值,该变量的名称表示为其他变量的值。尝试这个我recive tish错误消息:如何赋值给变量,哪个名字表示为其他变量bash?

Tesseract Open Source OCR Engine v3.04.00 with Leptonica 
Page 1 
Warning in pixReadMemTiff: tiff page 1 not found 
script.sh: line 39: SCORE_LIM=8000: command not found 
SCORE_LIM=216353 

的代码部分:

function RecognizeNumber #imagename varoutput 
{ 
    tesseract tmp/$1.tif tmp/$1 -psm 8 nobatch digits > tmp/debug_$1.txt 
    "$2"=$(head -n1 tmp/$1.txt | tr -d ' ' | tr -d '-' | tr -d '.') 
} 

就像你看到的我想assignt一定的参考价值$2这是在其他功能使用:

function ReadScore #geo imgname varoutput sleeptime 
{ 
    sleep $4 
    Crop $1 $2 
    MakeReadible $2 
    RecognizeNumber $2 $3 
    echo "$3=$$3" 
} 

在这里,我再次尝试相同的:echo "$3=$$3"和最可怕的部分是$$3,我刚刚写道,但它做了t test it (as think it will be value of var with name $ 3`)。

在代码中,我调用这个函数为:

ReadScore 135x35+110+130 score SCORE_LIM 1 

是正方体送花儿给人显示终端输出我试着用> tmp/debug_$1.txt这样做的另一个问题,但无论如何它appers。

我认为我做错了。 :D请帮助我!

回答

-1

我找到了解决方案,eval命令和${!variable}建设:

function RecognizeNumber #imagename varoutput 
{ 
    tesseract tmp/$1.tif tmp/$1 -psm 8 nobatch digits > tmp/debug_$1.txt 
    eval $2=$(head -n1 tmp/$1.txt | tr -d ' ' | tr -d '-' | tr -d '.') 
} 

function ReadScore #geo imgname varoutput sleeptime 
{ 
    sleep $4 
    Crop $1 $2 
    MakeReadible $2 
    RecognizeNumber $2 $3 
    echo "$3=${!3}" 
} 
0

使用printf;它比使用eval更安全,这可能会导致正在执行的值产生意外的代码。

function RecognizeNumber #imagename varoutput 
{ 
    tesseract tmp/$1.tif tmp/$1 -psm 8 nobatch digits > tmp/debug_$1.txt 
    printf -v "$2" "$(head -n1 tmp/$1.txt | tr -d ' .-')" 
} 

此外,您还可以获取和修改的值,而无需使用外部命令(始终引用您的参数扩展):

RecognizeNumber() { 
    tesseract tmp/"$1".tif tmp/"$1" -psm8 nobatch digits > tmp/debug_"$1".txt 
    read -r value < tmp/"$1".txt 
    printf -v "$2" "${value//[ .-]}" 
} 

间接参数扩展,${!name},是安全的。

function ReadScore #geo imgname varoutput sleeptime 
{ 
    sleep "$4" 
    Crop "$1" "$2" 
    MakeReadible "$2" 
    RecognizeNumber "$2" "$3" 
    echo "$3=${!3}" 
}