hrishikeshp19
hrishikeshp19

Reputation: 9028

error in shell script: unexpected end of file

The following script is showing me "unexpected end of file" error. I have no clue why am I facing this error. My all the quotes are closed properly.

#!/usr/bin/sh
insertsql(){
        #sqlite3 /mnt/rd/stats_flow_db.sqlite <<EOF
        echo "insert into flow values($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14,$15,$16,$17,$18)"
        #.quit
}

for i in {1..100}
do
        src_ip = "10.1.2."+$i
        echo $src_ip
        src_ip_octets = ${src_ip//,/}
        src_ip_int = $src_ip_octets[0]*1<<24+$src_ip_octets[1]*1<<16+$src_ip_octets[2]*1<<8+$src_ip_octets[3]

        dst_ip = "10.1.1."+$i
        dst_ip_octets = ${dst_ip//,/}
        dst_ip_int = $dst_ip_octets[0]*1<<24+$dst_ip_octets[1]*1<<16+$dst_ip_octets[2]*1<<8+$dst_ip_octets[3]

        insertsql(1, 10000, $dst_ip, 20000, $src_ip, "2012-08-02,12:30:25.0","2012-08-02,12:45:25.0",0,0,0,"flow_a010105_a010104_47173_5005_1_50183d19.rrd",0,12,$src_ip_int,$dst_ip_int,3,50000000,80000000)

done

Upvotes: 0

Views: 657

Answers (4)

rush
rush

Reputation: 2564

That is because you don't follow shell syntax. To ser variable you are not allowed to use space around = and to concatenate two parts of string you shouldn't use +. So the string

src_ip = "10.1.2."+$i

become

src_ip="10.1.2.$i"

Why you're using the string

src_ip_octets = ${src_ip//,/}

I don't know. There is absolutely no commas in you variable. So even to delete all commas it should look like (the last / is not required in case you're just deleting symbols):

    src_ip_octets=${src_ip//,}

The next string has a lot of symbols that shell intepreter at its own way and that's why you get the error about unexpected end of file (especially due to heredoc <<)

    src_ip_int = $src_ip_octets[0]*1<<24+$src_ip_octets[1]*1<<16+$src_ip_octets[2]*1<<8+$src_ip_octets[3]

So I don't know what exactly did you mean, though it seems to me it should be something like

    src_ip_int=$(( ${src_ip_octets%%*.}+$(echo $src_ip_octets|sed 's/[0-9]\+\.\(\[0-9]\+\)\..*/\1/')+$(echo $src_ip_octets|sed 's/\([0-9]\+\.\)\{2\}\(\[0-9]\+\)\..*/\1/')+${src_ip_octets##*.} ))

The same stuff is with the next strings.

Upvotes: 1

perreal
perreal

Reputation: 97938

A couple of problems:

  • There should be no space between equal sign and two sides of an assignment: e.g.,: dst_ip="10.1.1.$i"

  • String concatenation is not done using plus sign e.g., dst_ip="10.1.1.$i"

  • There is no shift operator in bash, no <<: $dst_ip_octets[0]*1<<24 can be done with expr $dst_ip_octets[0] * 16777216 `

  • Functions are called just like shell scripts, arguments are separated by space and no parenthesis: insertsql 1 10000 ...

Upvotes: 1

kkeller
kkeller

Reputation: 3247

That error is caused by <<. When encountering that, the script tries to read until it finds a line which has exactly (starting in the first column) what is found after the <<. As that is never found, the script searches to the end and then complains that the file ended unexpectedly.

That will not be your only problem, however. I see at least the following other problems:

  • You can only use $1 to $9 for positional parameters. If you want to go beyond that, the use of the shift command is required or, if your version of the shell supports it, use braces around the variable name; e.g. ${10}, ${11}...
  • Variable assignments must not have whitespace arount the equal sign
  • To call your insertsql you must not use ( and ); you'd define a new function that way.
  • The cass to your insertsql function must pass the parameters whitespace separated, not comma separated.

Upvotes: 3

larsks
larsks

Reputation: 311426

You can't do this:

        dst_ip_int = $dst_ip_octets[0]*1<<24+$dst_ip_octets[1]*1<<16+$dst_ip_octets[2]*1<<8+$dst_ip_octets[3]

The shell doesn't do math. This isn't C. If you want to do this sort of calculation, you'll need to use something like bc, dc or some other tool that can do the sort of math you're attempting here.

Most of those operators are actually shell metacharacters that mean something entirely different. For example, << is input redirection, and [ and ] are used for filename globbing.

Upvotes: 0

Related Questions