加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

Shell脚本语法错误:意外的文件结束

发布时间:2020-12-15 18:25:54 所属栏目:安全 来源:网络整理
导读:在以下脚本中,我收到一个错误: syntax error: unexpected end of file 这个错误是怎么回事?它正在指向该函数被调用的行. #!/bin/shexpected_diskusage="264"expected_dbconn="25"expected_httpdconn="20"expected_cpuusage="95"#expected_fd="100"httpdcon
在以下脚本中,我收到一个错误:

syntax error: unexpected end of file

这个错误是怎么回事?它正在指向该函数被调用的行.

#!/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 "$daten" > /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_usagenConfigured disk usage is    $expected_diskusagennnnn";
    #Checking for CPU usage
    if [ $cpu_usage -ge $expected_cpuusage]    ##{x%?}
    then
        echo "CPU usage exceeded";
        if [ $message -ne "" ]
        then
            message="$messagennCPU usage exceeded configured usage limit nCurrent CPU usage is $cpu_usagenConfigured CPU usage is $expected_cpuusagennnnn";
        else
            message="CPU usage exceeded configured usage limit nCurrent CPU usage is   $cpu_usagenConfigured CPU usage is $expected_cpuusagennnnn";
        fi ;
    fi
    #Checking for httpd connections
    if [ $httpdconn -ge $expected_httpdconn]    ##{x%?}
    then
        echo "HTTPD connections exceeded";
        if [ $message -ne "" ]
        then
            message="$messagennHTTPD connections exceeded configured usage limit nCurrent HTTPD connections is $httpdconnnConfigured HTTPD connection is $expected_httpdconn";
        else
            message="HTTPD connections exceeded configured usage limit nCurrent HTTPD connections is $httpdconnnConfigured HTTPD connection is $expected_httpdconn";
        fi ;
    fi ;
    message="$messagennnnn";
    value=$(cld_alert $message $httpdconn $cpu_usage $disk_usage $db_connections)
当您的结构不匹配时,通常会出现此错误 – 也就是说,您没有匹配的双引号,匹配的单引号,没有关闭一个控件结构,例如缺少一个if的fi,或者一个缺少一个用于.

发现这些错误的最佳方法是使用正确的缩进,这将显示您的控制结构的破坏以及语法高亮,这将显示引号不匹配的位置.

在这种特殊情况下,我可以看到你错过了一个fi.在你的代码的后半部分,你有5个ifs和4个fis.但是,您还有一些其他问题 – 您的反引号触摸/tmp/alert.txt …命令在语法上无效,并且在if测试的结束括号之前需要一个空格.

清理您的代码,错误开始脱颖而出.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读