line 10:syntax error: unexpected end of file

在Linux运行如下脚本的时候:

点击(此处)折叠或打开

  1. #/bin/bash
  2. echo "You are logged in as `whoami`"
  3. if [ `whoami` != root ]; then
  4. echo "Must be logged on as root to run this script."
  5. fi
遇到如下错误:
line 10:syntax error: unexpected end of file

多方测试始终不见效果,最终同事陈大师找到了原因,我的脚本是从Windows上ftp到Linux上的,Linux下的文件结束符和Windows下的文件结束符不一样。在Windows上编辑的脚本,放在Linux额上运行就会有这种问题。
网上又查了以下,解决方法是使用vim打开脚本,执行命令:
set fileformat=unix
保存退出,再次执行就没有问题了。当然,也可以在Linux上重新新建一个脚本。


请使用浏览器的分享功能分享到微信等