语法错误接近意外令牌“elif”

时间:2021-09-12 00:59:21
./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

Also this,

也这样,

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

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

What is wrong with this? It seems to be correct. I tried so many variations and cannot figure out what is wrong. Is there some kind of invisible character? If so, is there a command to strip it?

这有什么问题?这似乎是正确的。我尝试了这么多的变化,却搞不清楚哪里出了问题。是否存在某种无形的特质?如果是的话,是否有命令将其删除?

Edit: When I add #!/bin/bash at the top, I get the following error:

编辑:当我添加#!/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

5 个解决方案

#1


30  

It's your line endings. Transferring it from Windows has left the CR/LF line endings on.

这是你的结局。从Windows转移它已经留下了CR/LF行结束。

When I create a script then manually add the CR characters, I get exactly the same error:

当我创建一个脚本,然后手工添加CR字符时,我得到了完全相同的错误:

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

You can fix it by removing the CR character from CR/LF line endings.

您可以通过从CR/LF行结尾删除CR字符来修复它。

cat script.sh | sed '/\015/d' >newscript.sh
  • CR character correspond to \015 octal representation as listed in ASCII
  • CR字符对应于ASCII中列出的\015八进制表示

#2


3  

it looks like you've got the "dos problem", embedded control-M's in your file. fix it with sed:

看起来你遇到了“dos问题”,在你的文件中嵌入控件m。修复它对话:


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

#3


0  

Now that you've added the additional error message, I have a thought: the ^M is \r, which is the Mac OS X line ending or part of the Windows line ending - Linux uses \n only as EOL. If you edit in vim, you should be able to see the ^M if it's not right through the file.

现在您已经添加了额外的错误消息,我有一个想法:^ M \ r,Mac OS X线结束——Linux或Windows的一部分行结束符使用\ n只作为终点。如果你在vim编辑,您应该能够看到^如果它是不正确的文件。

#4


0  

Two ways to resolve this

有两种方法可以解决这个问题

1) Using Sed:-

1)使用Sed:-

Syntax

语法

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

2) Using dos2unix command

2)使用dos2unix命令

Syntax

语法

dos2unix fileName.txt fileName.txt

#5


-1  

I got below error in my mail when i set up cron for magento.

当我为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 >'

I found solution for that is i remove newline space from my cron1.php file. and its work.

我找到了解决方案,就是从cron1中删除换行空间。php文件。和它的工作。

(source)

(源)

#1


30  

It's your line endings. Transferring it from Windows has left the CR/LF line endings on.

这是你的结局。从Windows转移它已经留下了CR/LF行结束。

When I create a script then manually add the CR characters, I get exactly the same error:

当我创建一个脚本,然后手工添加CR字符时,我得到了完全相同的错误:

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

You can fix it by removing the CR character from CR/LF line endings.

您可以通过从CR/LF行结尾删除CR字符来修复它。

cat script.sh | sed '/\015/d' >newscript.sh
  • CR character correspond to \015 octal representation as listed in ASCII
  • CR字符对应于ASCII中列出的\015八进制表示

#2


3  

it looks like you've got the "dos problem", embedded control-M's in your file. fix it with sed:

看起来你遇到了“dos问题”,在你的文件中嵌入控件m。修复它对话:


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

#3


0  

Now that you've added the additional error message, I have a thought: the ^M is \r, which is the Mac OS X line ending or part of the Windows line ending - Linux uses \n only as EOL. If you edit in vim, you should be able to see the ^M if it's not right through the file.

现在您已经添加了额外的错误消息,我有一个想法:^ M \ r,Mac OS X线结束——Linux或Windows的一部分行结束符使用\ n只作为终点。如果你在vim编辑,您应该能够看到^如果它是不正确的文件。

#4


0  

Two ways to resolve this

有两种方法可以解决这个问题

1) Using Sed:-

1)使用Sed:-

Syntax

语法

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

2) Using dos2unix command

2)使用dos2unix命令

Syntax

语法

dos2unix fileName.txt fileName.txt

#5


-1  

I got below error in my mail when i set up cron for magento.

当我为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 >'

I found solution for that is i remove newline space from my cron1.php file. and its work.

我找到了解决方案,就是从cron1中删除换行空间。php文件。和它的工作。

(source)

(源)