首页 > 解决方案 > 在 bash 脚本中运行 if 语句时出错

问题描述

在这里,我编写了一个基本的 bash 脚本。我希望 if 块组织将输出写入的位置。我编写条件逻辑的方式是否存在语法错误?我得到的错误是针对我有 if 或 elif 语句的每一行。我相信这可能与 -eq 比较运算符有关,也可能与缺少或括号有关。

这是我遇到的错误的示例(终端中发生的最后一个错误;最接近底部):

./lit: line 17: [2: command not found

./lit: line 20: [2: command not found

./lit: line 23: [2: command not found

./lit:第 28 行:[3:找不到命令

./lit: line 31: [3: command not found

./lit: line 34: [3: command not found

./lit: line 37: [3: command not found

./lit:第 42 行:[3:找不到命令

./lit:第 45 行:[3:找不到命令

./lit:第 48 行:[3:找不到命令

./lit: line 51: [3: command not found

./lit: line 56: [3: command not found

./lit: line 59: [3: command not found

./lit: line 62: [3: command not found

./lit:第 65 行:[3:找不到命令

for i in {0..2};
do
    for j in {0..3};
    do
        for k in {0..3}
        do
            for l in {0..3}
            do

                START=$(date +%s.%N)
                  #some command here
                END=$(date +%s.%N)
                DIFF=$(echo "$END - $START" | bc)

                if ["$i" -eq "0"];
                then
                    echo "test$i$j$k$l $DIFF" >> repeatSample.txt;
                elif ["$i" -eq "1"]
                then 
                    echo "test$i$j$k$l $DIFF" >> writeZeroes.txt
                elif ["$i" -eq "2"]
                then
                    echo "test$i$j$k$l $DIFF" >> repeatPacket.txt
                fi

                if [$j -eq 0]
                then
                    echo "test$i$j$k$l $DIFF" >> byte15.txt
                elif [$j -eq 1]
                then
                    echo "test$i$j$k$l $DIFF" >> byte100.txt
                elif [$j -eq 2]
                then
                    echo "test$i$j$k$l $DIFF" >> byte500.txt
                elif [$j -eq 3]
                then
                    echo "test$i$j$k$l $DIFF" >> byte1000.txt
                fi

                if [$k -eq 0]
                then
                    echo "test$i$j$k$l $DIFF" >> loss10.txt
                elif [$k -eq 1]
                then
                    echo "test$i$j$k$l $DIFF" >> loss22.txt
                elif [$k -eq 2]
                then
                    echo "test$i$j$k$l $DIFF" >> loss37.txt
                elif [$k -eq 3]
                then
                    echo "test$i$j$k$l $DIFF" >> loss50.txt
                fi

                if [$l -eq 0]
                then
                    echo "test$i$j$k$l $DIFF" >> pinkpanth.txt
                elif [$l -eq 1]
                then
                    echo "test$i$j$k$l $DIFF" >> poeee.txt
                elif [$l -eq 2]
                then
                    echo "test$i$j$k$l $DIFF" >> dooors.txt
                elif [$l -eq 3]
                then
                    echo "test$i$j$k$l $DIFF" >> simplehead.txt
                fi

            done
        done
    done
done

标签: bashloopsif-statementnested-loops

解决方案


推荐阅读