意外令牌'elif'附近的语法错误 [英] Syntax error near unexpected token `elif'

查看:121
本文介绍了意外令牌'elif'附近的语法错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

./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

也是这个

./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?

编辑:当我在顶部添加#!/bin/bash时,出现以下错误:

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

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

推荐答案

这是您的行结尾.从Windows传输它已使CR/LF行的结尾保留.

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

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

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

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

您可以通过从CR/LF行尾删除CR字符来解决此问题.

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

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

  • CR字符对应于 ASCII
  • 中列出的\015八进制表示形式.

    • CR character correspond to \015 octal representation as listed in ASCII
    • 这篇关于意外令牌'elif'附近的语法错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆