-1

i have a script that looks something like

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

but when i run this script it saya

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

but the fi is there..i cant figure out why it is throwing the error this error does not come when i just have echo statements in the script

noob_coder
  • 749
  • 4
  • 15
  • 35
  • Is it possible that you have some whitespace following the here-doc end word? Look at your script with `cat -e file.sh` – glenn jackman Dec 06 '17 at 15:48
  • i have edited my question with cat -e script.sh output script – noob_coder Dec 06 '17 at 16:21
  • 1
    BTW, the `$`s make this harder to copy-and-paste (f/e, over to http://shellcheck.net/). – Charles Duffy Dec 06 '17 at 16:24
  • ...speaking of which, Shellcheck identifies rather a lot of potential issues. Please try to make a habit of cleaning those up *before* asking questions here. – Charles Duffy Dec 06 '17 at 16:25
  • (Also, if your goal is to identify plugins installed or removed, `diff` isn't really an ideal tool for that -- it does a lot of work to identify the smallest possible deltas, whereas all you need is to know which lines were added and which lines were removed; `comm` is the right tool for doing that job efficiently). – Charles Duffy Dec 06 '17 at 16:27
  • (and running `cut` over and over to pick out different fields is an antipattern -- use `IFS=: read -r plug_x ver_x plug_y ver_y` to read colon-separated fields into different shell variables in just one step) – Charles Duffy Dec 06 '17 at 16:27
  • (...and generating SQL via string concatenation is a dangerous practice for all the usual reasons -- if someone has a plugin named `'; DROP TABLE whatever`, that has potential to be bad news). – Charles Duffy Dec 06 '17 at 16:29
  • See http://mywiki.wooledge.org/BashFAQ/001 for best practices for reading a file line-by-line in shell, and http://mywiki.wooledge.org/DontReadLinesWithFor re: why `for` is the wrong tool for the job *specifically*. – Charles Duffy Dec 06 '17 at 16:29
  • (also, you don't need to re-`export` PATH when changing it -- if the old value is exported, the new value will be too). – Charles Duffy Dec 06 '17 at 16:30
  • @panda, notice all the whitespace after the 2nd here-doc terminator? That is preventing the here-doc from being closed, so `fi` is part of the here-doc. The bash manual says this about here-docs: `...read input from the current source until a line containing only word (with no trailing blanks) is seen.` – glenn jackman Dec 06 '17 at 17:47
  • 1
    Possible duplicate of [here-document gives 'unexpected end of file' error](https://stackoverflow.com/questions/18660798/here-document-gives-unexpected-end-of-file-error) – Benjamin W. Dec 06 '17 at 18:17
  • 1
    try to keep all the lines starting from line containing < – Jithin Scaria Dec 07 '17 at 12:33
  • thank you all for the answers..it was an indentation issue...i indented the code properly and it solved the issue – noob_coder Dec 27 '17 at 05:27

1 Answers1

0

I would suggest that if you are just running that one query to insert into table, then echo the query and pipe it to the mysql command, that would be (in my opinion) a better and efficient choice.

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

That should do the job or if you'd like to use While loop, you can certainly do so.

#!/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
Arpan
  • 71
  • 2