第21行:意外令牌“完成”附近的语法错误,然后:第21行:“完成”

时间:2016-05-23 01:40:05

标签: bash

我在这个bash脚本中遇到了上述错误:

line 21: syntax error near unexpected token `done'
line 21: ` done'

我在其他问题中寻找像这里提到的空循环,我什么都没看到。知道为什么吗?

#!/bin/bash

while read -a line; do
    :
    for word in ${array[*]}; do
        let last_index=${#arr[*]}-1
        bool=false
        if [[ $word != [1-9]* && bool==true ]]; then
            echo -n $word
        else if [[ $word != [1-9]* && bool==false ]]; then
                bool=false
                echo -n "_$word"
        else if [[ $word == ${array[last_index]} ]]; then
                date=${word//./ }
                for element in $date; do
                    element=${element/0}
                done
                echo " $date"
        else echo -n " $word "
        fi
    done
done

1 个答案:

答案 0 :(得分:3)

您只有一个 Array ( [0] => Array ( [id] => 1 [parent] => 0 [value] => hello [child] => Array ( [0] => Array ( [id] => 2 [parent] => 1 [value] => hi [child] => Array ( [0] => Array ( [id] => 3 [parent] => 2 [value] => whats up [child] => Array ( [0] => Array ( [id] => 4 [parent] => 3 [value] => yow ) ) ) [1] => Array ( [id] => 6 [parent] => 2 [value] => nice ) ) ) [1] => Array ( [id] => 5 [parent] => 1 [value] => hellow ) ) ) [1] => Array ( [id] => 7 [parent] => 0 [value] => great ) ,其中有三个fi语句,因此不需要if。您需要使用done而不是elif

else if

或者您需要更多地缩进第二个和第三个#!/bin/bash while read -a line; do : for word in ${array[*]}; do let last_index=${#arr[*]}-1 bool=false if [[ $word != [1-9]* && bool==true ]]; then echo -n $word elif [[ $word != [1-9]* && bool==false ]]; then bool=false echo -n "_$word" elif [[ $word == ${array[last_index]} ]]; then date=${word//./ } for element in $date; do element=${element/0} done echo " $date" else echo -n " $word " fi done done 语句,并添加缺少的if标记。

顺便说一句,冒号命令是无害的,但在此脚本中没有任何用处。