执行shell脚本遇到错误syntax error: unexpected "then" (expecting "}")

今天执行脚本的时候遇到错误,如下图:

root@ApFree:/usr/sbin# ./conntrack_num_graph.sh
./conntrack_num_graph.sh: line 9: syntax error: unexpected "then" (expecting "}")
root@ApFree:/usr/sbin# 

我就纳闷了,这个脚本几天前是能正常执行的,今天执行怎么会出错,why?

cat脚本一看如下:

#!/bin/sh
SITENAME="apfree"聽聽聽聽聽 # change for your site
mkdir -p /mnt/rrd
#iptables -L traffic -vnxZ -t filter > /tmp/traffic.tmp
# $1 = ImageFile, $2 = Time in secs to go back, $3 = RRDfile, $4 = GraphText
CreateGraph ()
{
聽聽聽聽聽聽聽聽# only run, if no other rrdtool is running
聽聽聽聽聽聽聽聽if [ -n "$(ps | grep rrdtool | grep -v grep)" ];then
                        return
聽聽聽聽聽聽聽聽fi
聽
聽聽聽聽聽聽聽聽rrdtool graph "${1}" -a PNG -s -"${2}" -w 550 -h 240 -v "" \
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽‘DEF:count=‘${3}‘:conntrack_count:AVERAGE‘ \
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽‘AREA:count#32CD32:Conntrack number‘ \
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽‘HRULE:0#000000‘ -t "${4}"
}
聽
# build sum-graph
if [ ! -e /mnt/rrd/conntrack_num.rrd ] ; then
聽聽聽聽聽聽聽聽rrdtool create /mnt/rrd/conntrack_num.rrd -s 300 \
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽DS:conntrack_count:GAUGE:600:0:600000000 \
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽RRA:AVERAGE:0.5:1:600
fi
CONNTRACK_COUNT=`cat /proc/sys/net/netfilter/nf_conntrack_count`
rrdtool update /mnt/rrd/conntrack_num.rrd N:$CONNTRACK_COUNT
CreateGraph /mnt/rrd/conntrack_num.png 86400 /mnt/rrd/conntrack_num.rrd "conntrack from $SITENAME"

尼玛,这下才明白,有乱码!怪不得会出错呢!

为什么会有乱码呢?哦,原来是因为我之前把这个脚本删除了,然后从麦库记事本上把代码直接copy过来重新建立的脚本,才会出现这种情况!

解决办法:把乱码全部删除掉,再执行,就ok了,只不过这样真心很麻烦呀,如果看官们有更方便的办法,还请告诉我呀。

执行shell脚本遇到错误syntax error: unexpected "then" (expecting "}")

时间: 2024-08-22 15:53:33

执行shell脚本遇到错误syntax error: unexpected "then" (expecting "}")的相关文章

shell 报错:syntax error: unexpected end of file

有时执行脚本时会报错: [[email protected] shell]# sh -x test.sh + $'\r' : command not found test.sh: line 37: syntax error: unexpected end of file 原因可能是: 文本编辑是在windows下通过记事本或其它编辑器编写的这个文件,运行 在cygwin模拟LINUX软件环境下. 解决思路: DOS下文件和Linux下文件格式差异问题导致的. DOS下的文本文件是以\r\n作为断

同一脚本sh 脚本名 报Syntax error: "(" unexpected而./脚本名不报错,求解!!

同一脚本sh 脚本名 执行时报Syntax error: "(" unexpected:而./脚本名执行不报错,为什么呢 脚本内容如下: function usage(){ echo "usage:$0 url" exit 1 } function check_url(){ wget --spider -q -o /dev/null --tries=1 -T 5 $1 if [ $? -eq 0 ] then echo "$1 is ok" el

php报错syntax error, unexpected T_GOTO, expecting T_STRING,报错文件与行数指向以下代码,是什么原因?

本机php版本是5.3.8,Apache/2.2.21public function goto($url, $msg=NULL) {if ($msg) {$this->jsAlert($msg);}$this->js('document.location="' . $url . '";');$this->output(true);exit;} 解决办法是将phpstudy版本号php-5.3.29改为php-5.2.17

shell脚本启动语法错误syntax error near unexpected token '{

执行shell脚本时失败,报语法错误,但脚本内容检查正常 原因为该脚本是在非Linux系统下编辑之后放到系统执行的,文件模式类型非Linux系统匹配的模式类型. 查看文件的模式类型 显示文件的模式类型为 dos 修改文件的模式类型为Linux匹配的 unix 保存之后即可正常启动脚本. shell脚本启动语法错误syntax error near unexpected token '{ 原文地址:https://www.cnblogs.com/zjfjava/p/10708370.html

SHELL syntax error:unexpected end of file 提示错误

SHELL syntax error:unexpected end of file 提示错误 if [ -n "$1" ] then echo "111" else echo "222" fi exit 0 执行sh my.sh par 提示syntax error:unexpected end of file.错误请问为什么啊? 文本编辑是在windows下通过记事本编写的这个文件,运行 在cygwin模拟LINUX软件环境下. 解决思路: D

linux的shell脚本报错“syntax error near unexpected token `”的解决

今天写了一个shell脚本,老报错检查了一下基本的语法并没有发现错误.后来经过百度解决掉了,现总结如下. 错误现象:执行shell脚本,老报错"syntax error near unexpected token `" 解决办法:vim -b example.sh打开文件,发现文件每一行的末尾多了一个^M,这个问题在使用vim example.sh查看是看不见的,只能通过报错来判断使用vim -b才能发现问题. 因为MS-DOS及Windows是回车+换行来表示换行,因此在Linux下

配置NDK Cygwin .bash_profile:行47: 语法错误: 未预期的文件结尾 syntax error: unexpected end of file 解决

可以在cygwin中通过vim修改,也可以在windows安装目录中修改 home\<你的用户名>\.bash_profile 文件中最后添加环境变量 NDKROOT=/cygdrive/e/Andriod/develop/android-ndk-r8 export NDKROOT 其中NDK=/cygdrive/<你的盘符>/<android ndk 目录> ,"NDK"这个名字随便起,以后经常用不要太长. 重启cygwin,输入: cd $NDK

PHP错误Parse error: syntax error, unexpected end of file in test.php on line 12解决方法

今天在写PHP程序的时候总是出现这样的错误:Parse error: syntax error, unexpected end of file in *.php on line *,然后我就根据提示,找到那个文件,然后错误中总是提示最后一行出错,我找到最后一行发现是</html>,晕的,这能有什么错误,找了好久才找到问题所在,拿来分享. 出现这个错误的原因就是语法错误,肯定是PHP程序的书写不规范造成,我后来一条一条看才发现,原来是PHP语句标识符错了,正常情况下应该是这样的:<?php

使用 trait 时报PHP Parse error: syntax error, unexpected &#39;use&#39; (T_USE) 这个错误

找一大圈原因, 最后终于找到了, 不是PHP版本的原因[], 是自己把use 写到了类里的方法里了. 这个东东,  不能脱离类单独使用, 否则的话, 会被认为是命名空间了. 测试例子如下 // Trait.php trait CustomerFunctionsTrait { public function plus ( $a = 1, $b = 1 ) { echo $a + $b; } public function minus ( $a = 5, $b = 1 ) { echo $a - $