bash 意外标记“elif”附近的语法错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4182122/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Syntax error near unexpected token `elif'
提问by Strawberry
./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
回答by paxdiablo
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 '/5/d' >newscript.sh
- CR character correspond to
\015
octal representation as listed in ASCII
- CR 字符对应
\015
于ASCII 中列出的八进制表示
回答by jcomeau_ictx
it looks like you've got the "dos problem", embedded control-M's in your file. fix it with sed:
看起来您遇到了“dos 问题”,文件中嵌入了 control-M。用 sed 修复它:
sed -i 's/\r//' chkf
回答by Sireesh Yarlagadda
Two ways to resolve this
解决这个问题的两种方法
1) Using Sed:-
1) 使用Sed:-
Syntax
句法
sed -i 's/\r//' filename.txt
2) Using dos2unixcommand
2) 使用dos2unix命令
Syntax
句法
dos2unix fileName.txt fileName.txt
回答by Chris Morgan
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 行结尾或 Windows 行结尾的一部分 - Linux 仅将 \n 用作 EOL。如果您在 中进行编辑vim
,您应该能够^M
通过文件查看它是否不正确。
回答by Ram Ram
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)
(来源)