在以下脚本中,我收到一个错误:
语法错误:意外的文件结束
这个错误是什么我可以恢复它?它指向调用函数的行.
#!/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)
Run Code Online (Sandbox Code Playgroud)
cam*_*amh 90
编辑:请注意,自编写此答案并重新格式化以来,原始帖子已被编辑.您应该查看历史记录以查看原始格式以了解此答案的上下文.
当您的结构不匹配时,通常会发生此错误 - 也就是说,您没有匹配的双引号,匹配单引号,没有关闭控件结构,例如缺少fi某个if或缺少done一个for.
发现这些的最佳方法是使用正确的缩进,它将显示您控制结构损坏的位置,以及语法突出显示,它将显示引号不匹配的位置.
在这种特殊情况下,我可以看到你错过了一个fi.在代码的后半部分,你有5 if秒和4 fi秒.但是,您还有许多其他问题 - 您的反引号touch /tmp/alert.txt...命令在语法上无效,并且在if测试的结束括号之前需要一个空格.
清理代码,错误开始凸显出来.
Dav*_*ony 18
就我而言,问题出在EOL转换中。(行结束)。
我在Windows上创建了文件,只有将EOL从Windows(CR LF)转换为Unix(LF)后,一切都进行顺利。
我很容易用Notepad ++从以下位置进行了转换:编辑-> EOL转换-> Unix(LF)
小智 10
在我的情况下,我发现放置一个here文档(如sqplus ... << EOF)语句缩进也会引发相同的错误,如下所示:
./dbuser_case.ksh: line 25: syntax error: unexpected end of file
Run Code Online (Sandbox Code Playgroud)
所以在删除了这个缩进后,它就没问题了.
希望能帮助到你...
与 OP 的问题无关,但我的问题是我是一个 noob shell 脚本编写者。我使用的所有其他语言都需要括号来调用方法,而 shell 似乎不喜欢那样。
function do_something() {
# do stuff here
}
# bad
do_something()
# works
do_something
Run Code Online (Sandbox Code Playgroud)
小智 5
我发现这有时是由于运行文件的MS Dos版本引起的。如果是这种情况,dos2ux应该可以解决该问题。
dos2ux file1 > file2
Run Code Online (Sandbox Code Playgroud)
使用块时的缩进会导致此错误并且很难找到。
if [ ! -d /var/lib/mysql/mysql ]; then
/usr/bin/mysql --protocol=socket --user root << EOSQL
SET @@SESSION.SQL_LOG_BIN=0;
CREATE USER 'root'@'%';
EOSQL
fi
Run Code Online (Sandbox Code Playgroud)
=> 上面的例子会导致错误,因为 EOSQL 是缩进的。删除缩进,如下所示。发布此信息是因为我花了一个多小时才找出错误。
if [ ! -d /var/lib/mysql/mysql ]; then
/usr/bin/mysql --protocol=socket --user root << EOSQL
SET @@SESSION.SQL_LOG_BIN=0;
CREATE USER 'root'@'%';
EOSQL
fi
Run Code Online (Sandbox Code Playgroud)