Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shell Script Syntax Error: Unexpected End of File [duplicate]

Tags:

shell

sh

In the following script I get an error:

syntax error: unexpected end of file

What is this error how can I resove it? It is pointing at the line whee the function is called.

#!/bin/sh  expected_diskusage="264" expected_dbconn="25" expected_httpdconn="20" expected_cpuusage="95" #expected_fd="100"  httpdconn=`ps -ef|grep -i httpd|grep -v grep|wc -l` #httpd connections cpu_usage=`ps aux|awk 'NR > 0 { s +=$3 }; END {print s}'` disk_usage=`df -h|awk {'print $2'}|head -n3|awk 'NF{s=$0}END{print s}'` #db_connections=`mysql -uroot -pexxxxxx -s -N -e "show processlist"|wc -l`  db_connections=6 cld_alert() {     nwconn=$1     cpu_usage=$2     disk_usage=$3     db_connections=$4     message=$5     `touch /tmp/alert.txt && > /tmp/alert.txt`     date=`date`     echo -e "$date\n" > /tmp/alert.txt     echo -e "$message" >> /tmp/alert.txt     path="/proc/$httpd/fd/";     cd $path     tfd=`ls -l|wc -l`;     sfd=`ls -ltr|grep sock|wc -l`;     echo "Total fds: $tfd" >> /tmp/alert.txt     echo "Socket fds: $sfd" >> /tmp/alert.txt     echo "Other fds: $[$tfd - $sfd]" >> /tmp/alert.txt       freememory=`vmstat | awk '{if (NR == 3) print "Free Memory:"\$4}'`;     echo "Free memory :$freememory" >> /tmp/alert.txt     Bufferedmemory=`vmstat | awk '{if (NR == 3) print "Buffered Memory:"\$5}'`;     echo "Buffered memory $Bufferedmemory" >> /tmp/alert.txt     CacheMemory=`vmstat | awk '{if (NR == 3) print "Cache Memory:"\$6}'`;     echo "Cache memory : $CacheMemory" >> /tmp/alert.txt     sshconn=`netstat -an|grep 22|wc -l`  #ssh connections     httpsconn=`netstat -an|grep 443|wc -l`  #https connections     wwwconn=`netstat -an|grep 80|wc -l`  #www connections     echo "Disk usage is $disk_usage" >> /tmp/alert.txt     echo "DB connections $db_connections" >> /tmp/alert.txt     echo "Network connections $nwconn" >> /tmp/alert.txt     echo "CPU Usage: $cpu_usage" >> /tmp/alert.txt     topsnapshot=`top -n 1 -b`     echo "===========================TOP COMMAND    SNAPSHOT====================================================";     echo "$topsnapshot" >> /tmp/alert.txt     echo"==================PS COMMAND SNAPSHOT=============================================================="     entireprocesslist=`ps -ef`     echo "$entireprocesslist" >> /tmp/alert.txt       echo Hello hi"";  }    message="" if [ ${disk_usage%?} -le $expected_diskusage ]    ##{x%?} Removes last character then     echo "disk usage exceeded";     message="Disk usage limit exceeded \nCurrent disk usage is $disk_usage\nConfigured disk usage is    $expected_diskusage\n\n\n\n\n";     #Checking for CPU usage     if [ $cpu_usage -ge $expected_cpuusage]    ##{x%?}     then         echo "CPU usage exceeded";         if [ $message -ne "" ]         then             message="$message\n\nCPU usage exceeded configured usage limit \nCurrent CPU usage is $cpu_usage\nConfigured CPU usage is $expected_cpuusage\n\n\n\n\n";         else             message="CPU usage exceeded configured usage limit \nCurrent CPU usage is   $cpu_usage\nConfigured CPU usage is $expected_cpuusage\n\n\n\n\n";         fi ;     fi     #Checking for httpd connections     if [ $httpdconn -ge $expected_httpdconn]    ##{x%?}     then         echo "HTTPD connections exceeded";         if [ $message -ne "" ]         then             message="$message\n\nHTTPD connections exceeded configured usage limit \nCurrent HTTPD connections is $httpdconn\nConfigured HTTPD connection is $expected_httpdconn";         else             message="HTTPD connections exceeded configured usage limit \nCurrent HTTPD connections is $httpdconn\nConfigured HTTPD connection is $expected_httpdconn";         fi ;     fi ;     message="$message\n\n\n\n\n";     value=$(cld_alert $message $httpdconn $cpu_usage $disk_usage $db_connections) 
like image 931
Rajeev Avatar asked Mar 27 '12 08:03

Rajeev


People also ask

How fix Unexpected end of file in Linux?

An Unexpected end of file error in a Bash script usually occurs when you there is a mismatched structure somewhere in the script. If you forget to close your quotes, or you forget to terminate an if statement, while loop, etc, then you will run into the error when you try to execute your Bash script.

How do you end a file in bash?

One of the many known methods to exit a bash script while writing is the simple shortcut key, i.e., “Ctrl+X”. While at run time, you can exit the code using “Ctrl+Z”.

What is Fi in shell script?

fi statement is the fundamental control statement that allows Shell to make decisions and execute statements conditionally.


2 Answers

Edit: Note that the original post has been edited since this answer was written and has been reformatted. You should look at the history to see the original formatting to understand the context for this answer.

This error occurs often when you have mismatched structure - that is, you do not have matching double quotes, matching single quotes, have not closed a control structure such as a missing fi with an if, or a missing done with a for.

The best way to spot these is to use correct indentation, which will show you where you have a broken control structure, and syntax highlighting, which will show you where quotes are not matched.

In this particular case, I can see you are missing a fi. In the latter part of your code, you have 5 ifs and 4 fis. However you also have a number of other problems - your backquoted touch /tmp/alert.txt... command is syntactically invalid, and you need a space before the closing bracket of an if test.

Clean up your code, and errors start to stand out.

like image 156
camh Avatar answered Oct 08 '22 18:10

camh


in my case the issue was in the EOL Conversion. (End Of Line).

i created the file on windows and only after i converted the EOL from windows(CR LF) to unix(LF), everything went well.

I did the conversion with Notepad++ very easily from: Edit -> EOL Conversion -> Unix(LF)

like image 36
David Gidony Avatar answered Oct 08 '22 19:10

David Gidony