shell腳本文件是dos格式,即每一行結尾以\r\n來標識,而unix格式的文件行尾則以\n來標識。
查看腳本文件是dos格式還是unix格式的幾種辦法。
(1)cat -A filename 從顯示結果可以判斷,dos格式的文件行尾爲^M$,unix格式的文件行尾爲$。
(2)od -t x1 filename 如果看到輸出內容中存在0d 0a的字符,那麼文件是dos格式,如果只有0a,則是unix格式。
(3)vi filename打開文件,執行 : set ff,如果文件爲dos格式在顯示爲fileformat=dos,如果是unxi則顯示爲fileformat=unix。
解決方法
方法一:
[root@localhost bin]# dos2unix run.sh
dos2unix: converting file run.sh to UNIX format ...
方法二:
vim mysell.sh
:get fileformat #查看本文件的格式
:set fileformat=unix #設置文件爲unix
:wq #保存
方法三:
#使用sed命令,直接替換結尾符爲unix格式
sed -i "s/\r//" run.sh
或者
sed -i "s/^M//" run..sh