2010-11-15 123 views
10
./chkf: line 30: syntax error near unexpected token `elif' 
'/chkf: line 30: `elif [ -f "$object" ] ; then 


if [ -d "$object" ] ; then 
    message="$message a directory" 
elif [ -f "$object" ] ; then 
    message="$message a regular file." 
else 
    message="$message not a known file type" 
fi 

而且此,語法錯誤附近意外的標記'的elif」

./chkf: line 38: syntax error near unexpected token `else' 
'/chkf: line 38: `else 

if [ -w "$object" ] ; then 
    write="writeable" 
else 
    write="not writeable" 
fi 

有什麼不對呢?這似乎是正確的。我嘗試了很多變化,無法弄清楚什麼是錯誤的。有沒有某種無形的角色?如果是這樣,是否有命令將其剝離?

編輯:當我在頂部加入#!/bin/bash,我得到以下錯誤:

interpreter "/bin/bash" not found 
file link resolves to "/usr/bin/bash" 
-bash: ./chkf: /bin/bash^M: bad interpreter: No such file or directory 
+0

你用的是什麼外殼?請發佈[最小測試用例](http://sscce.org/),而不是片段。 – outis 2010-11-15 07:04:58

+0

@outis我正在使用bash – Strawberry 2010-11-15 07:05:27

+0

什麼版本? 'echo $ BASH_VERSION' – outis 2010-11-15 07:06:31

回答

30

這是您的行尾。從Windows傳輸它已將CR/LF行結束。

當我創建一個腳本,然後手動添加CR人物,我得到完全相同的錯誤:

qq.sh: line 3: syntax error near unexpected token `elif' 
'q.sh: line 3: `elif [ 1 == 1 ] ; then 

你可以解決它:

cat script.sh | sed '/\015/d' >newscript.sh 

這將擺脫所有CR文件中的字符(15個八進制數是13位十進制數)。

+0

+1,尼斯方法。 – codaddict 2010-11-15 07:16:47

+3

好的。 'dos2unix'是安裝在許多系統上的一個方便的實用程序,可以移除現場的CR。 – 2010-11-15 08:39:19

+3

如果您使用vim作爲編輯器,您可以設置「:set ff = unix」並再次寫入文件。 – f4m8 2011-11-09 14:56:40

0

現在你已經添加了額外的錯誤信息,我有一個想法:在^M是用\ r ,這是Mac OS X行結尾或Windows行結尾的一部分 - Linux僅將\ n作爲EOL使用。如果您在vim中編輯,如果文件不正確,您應該能夠看到^M

+0

OS X像Linux和Unix一樣使用'\ n'(因爲它是家庭成員)。舊的Mac OS版本曾經使用過'\ r'。 – 2010-11-15 07:32:14

+0

@丹尼斯:我明白了;沒有意識到它已經改變了操作系統和OS X. – 2010-11-15 07:41:28

3

它看起來像你有「DOS問題」,嵌入式控制-M在你的文件中。使用sed修復它:


sed -i 's/\r//' chkf 
-1

當我爲magento設置cron時,在郵件中出現錯誤。

/bin/sh: -c: line 0: syntax error near unexpected token `newline' 
/bin/sh: -c: line 0: `php /home/pooja/public_html/magento/journal/cron1.php >' 

我找到解決方案,這是我從我的cron1.php文件中刪除換行符空間。 及其工作。

source

0

兩種方式來解決這個

1)使用桑達: -

語法

sed -i 's/\r//' filename.txt 

2)使用命令unix2dos

語法

dos2unix fileName.txt fileName.txt 
相關問題