运行shell脚本,报syntax error: unexpected end of file 或报syntax error in conditional expression 等情况

用notepad++写shell脚本,但是执行的时候会报错。

目前写的shell脚本都报过以下错误:

syntax error near unexpected token `$’do\r”

syntax error in conditional expression

syntax error: unexpected end of file

syntax error near unexpected token `fi'

拿其中一个shell来说明

#!/bin/bash
nbpdbfxjz_startdate=$(date -d -7-month +"%Y-%m")
nbpdbfxjz_enddate=$(date +"%Y-%m")
lastmonth_year=$(date -d -0-month +"%Y")
lastmonth_month_0=$(date -d -0-month +"%m")
bill_month=$(date -d -0-month +"%Y%m")
lastmonth_yearmonth_1=$(date -d -1-year-0-month +"%Y%m")
lastmonth_yearmonth_2=$(date -d -2-year-0-month +"%Y%m")
lastmonth_lastyear=$(date -d -1-year-0-month +"%Y")
lastmonth_yearmonth_04=$(date -d -1-year-0-month +"%Y")"04"
month_04="04"
if [ $(echo "$lastmonth_month_0 <= $month_04" | bc) -eq 1 ]
	then 
		lastmonth_lastyear=`expr $lastmonth_year - 2`
		lastmonth_yearmonth_04=`expr $lastmonth_year - 1`"04"
else 
		lastmonth_lastyear=`expr $lastmonth_year - 1`
		lastmonth_yearmonth_04=`expr $lastmonth_year`"04"
fi
echo ${bill_month}
echo ${lastmonth_yearmonth_1}
echo ${lastmonth_yearmonth_2}
echo ${lastmonth_yearmonth_04}
echo ${lastmonth_lastyear}
echo ${nbpdbfxjz_startdate}
echo ${nbpdbfxjz_enddate}

文件命名为sh_test.sh,然后把文件放到Linux服务器上,执行命令:sh sh_test.sh

报错 sh_test.sh: line 27: syntax error: unexpected end of file

网上也找了一下相关的资料,是Linux和windows下的有些符号不兼容的问题

有两种解决方法:

第一种,用notepad++将文件格式转为unix格式

 用notepad++打开shell文件,选择 编辑->文档格式转换->转为UNIX格式,然后再将文件发布到Lunix服务器上执行

第二种,Linux上用命令将文件格式转为unix格式

 输入 vim sh_test.sh

 输入 :set ff

 回车,可以看到当前文件格式是dos,现在要把格式改为unix,命令  :set ff=unix  或者  :set fileformat=unix

 输入 :set ff=unix

 回车,输入 :wq!

回车,这样就已经把格式改为unix了,当然可以验证一下

 输入 :set ff

会显示fileformat=unix

 

以上两种方法都可以将文件格式修改为unix格式,此时在执行文件 sh sh_test.sh

这样就能得到结果,不会报错了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值