if语句将错误作为文件结尾抛出

时间:2017-12-06 15:43:04

标签: shell

我的脚本看起来像

#!/bin/bash$
#x=new value$
#y=old value$
$
export PATH=/xxx/xxx/xxx:$PATH$
$
#get the difference of two files$
diff --side-by-side --suppress-common-lines file.txt file1.txt | tr -d "|,<,>,'\t'" | sed 's/      /:/g'  | sed 's/^://' > diff.txt$
cat diff.txt$
$
#get the values$
for i in `cat diff.txt`; do$
        plug_x=`echo $i | cut -d ":" -f1`$
        echo "the value of jenkins plugin is $plug_x"$
        ver_x=`echo $i | cut -d ":" -f2`$
        echo "the value of jenkins version is  $ver_x"$
        plug_y=`echo $i | cut -d ":" -f3`$
        echo "the value of db plugin is $plug_y"$
        ver_y=`echo $i | cut -d ":" -f4`$
        echo "the value of db version is $ver_y"$
        if [ -z "$ver_y" ]  && [ -z "$ver_x" ] ;$
        then $
                echo "the plugin is newly added"$
                #newly added plugin should be updated in the db$
        #       mysql -u root -ppassword -h server --local-infile db << EOFMYSQL$
                #update the table with the new version$
#EOFMYSQL$
        else$
                echo "the plugin has changes"$
                mysql -u root -ppassword -h server --local-infile db << EOFMYSQL$
                insert into table (xxx, xxx) values('$ver_x','$plug_x');$
$
EOFMYSQL        $
fi$
done$

但是当我运行这个脚本时,它会说

Syntax error: end of file unexpected (expecting "fi")

但是fi就在那里......我无法弄清楚为什么它会抛出错误 当我在脚本

中只有echo语句时,不会出现此错误

1 个答案:

答案 0 :(得分:0)

我建议如果你只是运行那个插入到表中的查询,那么回显查询并将其传递给mysql命令,这将是(在我看来)更好和更有效的选择。

#!/bin/bash

diff --side-by-side --supress-common-lines test1.txt test2.txt | tr -d "|,<,>,'\t'" | sed 's/      /:/g'  | sed 's/^://' > ./diff.txt

for i in `cat ./diff.txt`; do
    plug_x=`echo $i | cut -d ":" -f1`
    echo "the value of jenkins plugin is $plug_x"

    ver_x=`echo $i | cut -d ":" -f2`
    echo "the value of jenkins version is  $ver_x"

    plug_y=`echo $i | cut -d ":" -f3`
    echo "the value of db plugin is $plug_y"

    ver_y=`echo $i | cut -d ":" -f4`
    echo "the value of db version is $ver_y"

    if [ -z "$ver_y" ]  && [ -z "$ver_x" ]
    then
        echo "the plugin is newly added"
    else
        echo "the plugin has changes"
        echo "insert into table (xxx, xxx) values('$ver_x','$plug_x')" | mysql -u root -ppassword -h server --local-infile db
    fi
done

这应该可以完成这项工作,或者如果你想使用While循环,你当然可以这样做。

#!/bin/bash

diff --side-by-side --supress-common-lines test1.txt test2.txt | tr -d "|,<,>,'\t'" | sed 's/      /:/g'  | sed 's/^://' > ./diff.txt

cat ./diff.txt | while read i
do
    plug_x=`echo $i | cut -d ":" -f1`
    echo "the value of jenkins plugin is $plug_x"

    ver_x=`echo $i | cut -d ":" -f2`
    echo "the value of jenkins version is  $ver_x"

    plug_y=`echo $i | cut -d ":" -f3`
    echo "the value of db plugin is $plug_y"

    ver_y=`echo $i | cut -d ":" -f4`
    echo "the value of db version is $ver_y"

    if [ -z "$ver_y" ]  && [ -z "$ver_x" ]
    then
        echo "the plugin is newly added"
    else
        echo "the plugin has changes"
        echo "insert into table (xxx, xxx) values('$ver_x','$plug_x')" | mysql -u root -ppassword -h server --local-infile db
    fi
done