Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash 是否可以使用变量作为for循环表达式?_Bash - Fatal编程技术网

Bash 是否可以使用变量作为for循环表达式?

Bash 是否可以使用变量作为for循环表达式?,bash,Bash,我正在编写一个脚本,以合并漫画1110的xkcd网络喜剧瓷砖。到目前为止,我已经写了大约100行,而不是单独做图像的每个象限,我决定使用函数来做这件事,为每个象限重复,并为每个象限使用变量,使函数每次都有不同的作用。我希望能够像这样做,除了每次使用不同的值 a='((n=$north; n>=1; n--))'; for ${a}; do "echo Hello World!" done 我的问题是,我在为for循环表达式使用变量时遇到问题,它会出现“第8行:eval“$a”:不

我正在编写一个脚本,以合并漫画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
#/bin/bash
#创建日期:2012年9月20日
#由Jonathan Bondhus创建-https://github.com/jbondhus
#Andreas Reichinger负责图像瓷砖放置分析,xkcd.com负责漫画。使用Antonio Frascarelli的ImageMagick代码。
#
#此脚本将合并xkcd漫画编号1110“单击并拖动”中的文件。
函数函数_join(){
回声“#”;
如果[[象限-等式1]];则
回声“#第一象限:;#将第一象限连接在一起
函数u象限
其他的
如果[[象限-等式2]];则
回声“#第二象限:;#将第二象限连接在一起
函数u象限
其他的
如果[[象限-等式3]];则
回声“#第三象限:;#将第三象限连接在一起
函数u象限
其他的
如果[[象限-等式4]];则
回声“#第四象限:;#将第四象限连接在一起
函数u象限
fi
fi
fi
fi
回声“#”;
美元{a};
做
回显“#行编号“$n”…”;
兑换$b;
对于“eval”$c`;
做
如果[$d];#如果平铺文件不存在,将使用空方块。
然后转换为$e;
否则转换为$f;
fi;
完成;
转换$g;#将图像附加到当前象限
rm-f$h;
回声“#……好”;
回声“#”;
完成;
}
函数_象限(){
如果[[象限-等式1]];则#设置函数_join的变量
quadrantName=“第一个”
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='+附加结果$n''w.png$n''n$w''w.png结果$n''w.png'
f='+追加结果$n''w.png_blank.png结果$n''w.png'
g='-append${quadrantName}Quadrant.png结果$n''w.png${quadrantName}Quadrant.png'
h='resultn$n''w.png'
其他的
如果[[象限-等式2]];则
quadrantName=“秒”
a='n=$north;n>=1;n--'
b='-size 0x1 xc:white resultn$n''e.png'

c='e=$east;e恐怕不行。语法分析发生在参数扩展之前(毕竟,参数需要首先识别为参数)。我建议重新构造代码以传递数据,而不是可执行代码。

恐怕不行。语法分析发生在参数扩展之前(毕竟,参数需要首先识别为参数)。我建议重新构造代码以传递数据,而不是可执行代码

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

#!/bin/bash
north=5;
for ((n=$north; n>=1; --n))
do
        ((north--))
     echo "Hello World!  $north $n"
done
cat north.sh

#!/bin/bash
north=5;
for ((n=$north; n>=1; --n))
do
        ((north--))
     echo "Hello World!  $north $n"
done

那么你具体在想什么?写出非循环语句?或者嵌套的if语句?那么你具体在想什么?写出非循环语句?或者嵌套的if语句?不确定为什么不使用
elif
,或者更好的是,先用
case${quad})我不认为你需要所有的缩进,只是让你很难跟上你正在做的事情。那就是说,有趣的问题,考虑在AWK中改写一个偶然的调用,比如<代码> CMD=“转换”OPT1 OPT2 OPT3…;系统(CMD)。.
。祝你好运。不知道为什么不使用
elif
或者更好的是,
case${quad}首先……做……;第二)……;……EASC/<代码>。我不认为你需要所有的缩进,只是让你很难跟上你正在做的事情。那就说,有趣的问题,考虑在AWK中改写一个偶然的调用,比如<代码> CMD=“转换”OPT1 OPT2 OPT3…;系统(CMD);…< /代码>。祝你好运。