1. 程式人生 > >syntax error near unexpected token `do

syntax error near unexpected token `do

這種情況發生的原因是因為你所處理的檔案換行符是dos格式的"\r\n"
可以使用
cat -v 檔名

來檢視換行符是否是。如果是上述的,則行結尾會是^m,需要轉換成linux/unix格式的"\n"。

#!/bin/sh^M ^M #frist^M #test script ^M #auther data^M ^M for file in *^M do^M
if grep -q POSIX $file^M
then^M
echo $file^M
fi^M done^M ^M

具體轉換辦法就是轉換換行符 可以用sed命令處理一下檔案,命令如下:

sed 's/\r//' 原檔案 > 轉換後文件
這是不同系統編碼格式引起的:在windows系統中編輯的.sh檔案可能有不可見字元,所以在Linux系統下執行會報以上異常資訊。