2010-11-15 85 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 
相关问题