linux脚本相关: Syntax error: end of file unexpected (expecting "then") 提示错误

今天写了个linux脚本,判断某进程是否已经运行,代码如下:

#!/bin/sh

pid=$(ps aux | grep "java -jar hello.jar" | grep -v "grep"  | sed -n '1P' | awk '{print $2}' ) 


if [ -z $pid ] ; then  

echo "there isn't this process!" else 

echo $pid fi 

如法应该是ok的,但总是报错:  Syntax error: end of file unexpected (expecting "then")

上网查了下  大概是因为我在windows下些的脚本,windows和linux的回车换行时不一样的字符,所以脚本在解释时会出现问题。

在windows里,换行用的两个符号,回车换\r行符号\n,在linux下只需一个符号\n就可以了. 

有个叫dos2unix 的工具,可以解决这问题。用法是这样的:dos2unix filename;

但我用的是ubuntu系统

Ubuntu 下默认没有安装dos2unix工具,而且也没有一个叫这个名字的工具(我在solaris里用过dos2unix,不知道为啥Ubuntu没有)。但是有一个替代工具——tofrodos , 下面就说一下它的安装和使用。
当然还是apt安装啦。

(1)安装tofrodos
sudo apt-get install tofrodos 
实际上它安装了两个工具:todos(相当于unix2dos),和fromdos(相当于dos2unix)


安装完即可,现在你已经可以进行文本格式的转换啦。 
比如: todos Hello.txt (即unix2dos Hello.txt) 
原文地址:https://www.cnblogs.com/langlang/p/1889664.html