2017-12-06 4 views
-1

の終了などのエラーをスローした場合、私は文がファイル

#!/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ありthere..iカントあるスクリプトを持っていますなぜそれがエラーを投げているのかわかりません 私はスクリプト内にecho文を持っているだけで、このエラーは出ません。

+0

この文書の末尾に続く空白がある可能性はありますか? 'cat -e file.sh'を使ってあなたのスクリプトを見てください –

+0

cat -e script.shの出力スクリプトで私の質問を編集しました – panda

+1

ところで、' $ 'はこれをコピーアンドペースト(f/e、 http://shellcheck.net/に)。 –

答えて

0

私は、クエリをエコーし​​、それをmysqlコマンドにパイプすると、(私の意見では)より効率的な選択肢になります。仕事をすべきか、Whileループを使用したい場合、あなたは確かにそうすることができ

#!/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 

#!/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 
関連する問題