1

我正在编写一个脚本来合并漫画 1110 的 xkcd 网络漫画图块。到目前为止,我已经写了大约 100 行,而不是分别处理图像的每个象限,我决定使用函数来执行此操作,重复每个象限并为每个象限使用变量以使函数每次都以不同的方式运行。我希望能够做到这样的事情,除了每次都使用不同的值。

a='((n=$north; n>=1; n--))';
for ${a};
    do "echo Hello World!"
done

我的问题是,我在为 for 循环表达式使用变量时遇到了麻烦,它会出现“第 8 行:eval “$a”':不是有效的标识符”,如果我尝试将它放在括号中,它会出现“第 30 行:语法错误:需要算术表达式”,然后是“第 30 行:语法错误:`((${a}))”。这就是我到目前为止所拥有的。在任何人回答之前,是的,我知道它有错误。

#!/bin/bash
# Date Created: September 20th, 2012
# Created by Jonathan Bondhus - https://github.com/jbondhus
# Credit to Andreas Reichinger for analysis of image tile placement, xkcd.com for comic. Used ImageMagick code from Antonio Frascarelli.
#
#  This script will merge the files from xkcd comic number 1110, "Click and Drag".

function function_join() {
    echo "#";
    if [[ quadrant -eq 1 ]]; then
    echo "# First Quadrant:"; # Join together the first quadrant
    function_quadrant
    else
        if [[ quadrant -eq 2 ]]; then
        echo "# Second Quadrant:"; # Join together the second quadrant
        function_quadrant
        else
            if [[ quadrant -eq 3 ]]; then
            echo "# Third Quadrant:"; # Join together the third quadrant
            function_quadrant
            else
                if [[ quadrant -eq 4 ]]; then
                echo "# Fourth Quadrant:"; # Join together the fourth quadrant
                function_quadrant
                fi
            fi
        fi
    fi
    echo "#";
    for ${a};
    do
        echo "# row number "$n" ...";
        convert $b;
        for `eval "$c"`;
        do
            if [ $d ];      # If the tile file doesn't exist, an empty square will be used.
                then convert $e;
                else convert $f;
            fi;
        done;
        convert $g;    # Append the image onto the current quadrant
        rm -f $h;
        echo "# ... OK";
        echo "#";
    done;
}

function function_quadrant() {
    if [[ quadrant -eq 1 ]]; then # Set the variables for function_join
    quadrantName="first"
    a='((n=$north; n>=1)); n--'
    b='-size 0x1 xc:white resultn$n''w.png'
    c='((w=$west; w>=1; w--))'
    d='-e $n''n$w''w.png'
    e='+append resultn$n''w.png $n''n$w''w.png resultn$n''w.png'
    f='+append resultn$n''w.png _blank.png resultn$n''w.png'
    g='-append ${quadrantName}Quadrant.png resultn$n''w.png ${quadrantName}Quadrant.png'
    h='resultn$n''w.png'
    else
        if [[ quadrant -eq 2 ]]; then
        quadrantName="second"
        a='n=$north; n>=1; n--'
        b='-size 0x1 xc:white resultn$n''e.png'
        c='e=$east; e<=20; e++'
        d='-e $n''n$e''e.png'
        e='+append resultn$n''e.png $n''n$e''e.png resultn$n''e.png'
        f='+append resultn$n''e.png _blank.png resultn$n''e.png'
        g='-append ${quadrantName}Quadrant.png resultn$n''e.png ${quadrantName}Quadrant.png'
        h='resultn$n''e.png'
        else
            if [[ quadrant -eq 3 ]]; then
            quadrantName="third"
            a='s=1; s<=$south; s++'
            b='-size 0x1 xc:black results$s''w.png'
            c='w=$west; w>=1; w--'
            d='-e $s''s$w''w.png'
            e='+append results$s''w.png $s''s$w''w.png results$s''w.png'
            f='+append results$s''w.png _black.png results$s''w.png'
            g='-append ${quadrantName}Quadrant.png results$s''w.png ${quadrantName}Quadrant.png'
            h='results$s''w.png'
            else
                if [[ quadrant -eq 4 ]]; then
                quadrantName="fourth"
                a='s=1; s<=$south; s++'
                b='-size 0x1 xc:black results$s''e.png'
                c='e=1; e<=20; e++'
                d='-e $s''s$e''e.png'
                e='+append results$s''e.png $s''s$e''e.png results$s''e.png'
                f='+append results$s''e.png _black.png results$s''e.png'
                g='-append ${quadrantName}Quadrant.png results$s''e.png ${quadrantName}Quadrant.png'
                h='results$s''e1.png'
                else # If more than 4 quadrants, just to be safe, exit
                    exit 1
                fi
            fi
        fi
    fi
}

for (( quadrant = 0; quadrant < 4; quadrant++ )); do
    function_join
done

echo "#################################"
echo "Joining completed!"
echo "#################################"

exit 0
4

2 回答 2

1
./north.sh 
Hello World!  4 5
Hello World!  3 4
Hello World!  2 3
Hello World!  1 2
Hello World!  0 1

北猫.sh

#!/bin/bash
north=5;
for ((n=$north; n>=1; --n))
do
        ((north--))
     echo "Hello World!  $north $n"
done
于 2012-09-22T16:07:58.097 回答
0

怕不是。语法解析发生在参数扩展之前(毕竟参数需要首先被识别)。我建议重组您的代码以传递数据,而不是可执行代码。

于 2012-09-21T16:08:09.653 回答