用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
这样就能得到结果,不会报错了