bash 文件返回意外的令牌`$'do\r''
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27176781/
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
bash file returns unexpected token `$'do\r''
提问by kfirba
I found this script online and tried to use it:
我在网上找到了这个脚本并尝试使用它:
#!/bin/sh
# Target directory
TARGET=
echo "Copying to $TARGET"
for i in $(git diff --name-only )
do
# First create the target directory, if it doesn't exist.
mkdir -p "$TARGET/$(dirname $i)"
# Then copy over the file.
cp "$i" "$TARGET/$i"
done
echo "Done";
I've validated the script online, and the script is okay. I've also tried to change it in various ways, but it doesn't work for me.
我已经在线验证了脚本,脚本没问题。我也尝试过以各种方式改变它,但它对我不起作用。
I've also tried running something like:
我也试过运行类似的东西:
#!/bin/sh
# Target directory
TARGET=
echo "Copying to $TARGET"
for i in $(ls)
do
echo "text"
done
And I still get the same error:
我仍然得到同样的错误:
./git-copy.sh: line 6: syntax error near unexpected token `$'do\r''
'/git-copy.sh: line 6: `do
Why is that?
这是为什么?
回答by Elliott Frisch
Your script has been edited on a DOS or Windows based system and contains carriage-return characters that Linux/Unix does not like (that what \r
is). You could use dos2unix
to convert the carriage return line endings to the correct format; if you don't have dos2unix
you might use awk
like
您的脚本已在基于 DOS 或 Windows 的系统上编辑过,并且包含 Linux/Unix 不喜欢的回车字符(这就是什么\r
)。您可以使用dos2unix
将回车行结尾转换为正确的格式;如果你没有dos2unix
你可以使用awk
像
awk '{ sub("\r$", ""); print }' git-copy.sh > git-copy2.sh
mv git-copy2.sh git-copy.sh
回答by Bfcm
Another possible solution using unix text editor vi:
使用 unix 文本编辑器 vi 的另一种可能的解决方案:
open file in vi edit with vi filename.sh
command;
使用vi filename.sh
命令在 vi 编辑中打开文件;
type in vi :set ff=unix
command;
输入vi:set ff=unix
命令;
save file with :wq
保存文件 :wq
It will save the file with unix line endings.
它将保存带有 unix 行结尾的文件。
回答by JRichardsz
This worked for me:
这对我有用:
dos2unix my_script.sh
If you don't have dos2unix :
如果您没有 dos2unix :
For CentOS, Fedora or RHEL:
对于 CentOS、Fedora 或 RHEL:
$ sudo yum install dos2unix
For Ubuntu or Debian:
对于 Ubuntu 或 Debian:
$ sudo apt-get install tofrodos
$ sudo ln -s /usr/bin/fromdos /usr/bin/dos2unix
Explanation
解释
This commonly is caused when the executable file is created in an operative system (windows) and used in another operative system (Linux). Also rare text editors could be the problem.
这通常是在可执行文件在操作系统 (windows) 中创建并在另一个操作系统 (Linux) 中使用时引起的。罕见的文本编辑器也可能是问题所在。
回答by Cookie
Just change your line sequence in your text editor from CRLF to LF. It worked for me :)
只需将文本编辑器中的行序列从 CRLF 更改为 LF。它对我有用:)
for notepad++: edit => EOL Conversion => Unix(LF)
对于记事本++:edit => EOL Conversion => Unix(LF)
回答by JJoao
This is a rather common nightmare :)
这是一个相当普遍的噩梦:)
I built a script to normalize \r\n
from Dos and from (old)Mac; it also removes BOM
if present:
我构建了一个脚本来\r\n
从 Dos 和(旧)Mac进行标准化 ;如果存在,它还会删除 BOM:
#!/usr/bin/perl -pi
s/2\r|\r2|2|\r/2/g; ## normalize line endings
s/^(\xFF\xFE|\xFE\xFF|\xEF\xBB\xBF)//; ## remove common initial BOM
Usage: script textualfile*
用法: script textualfile*
Ok, ok: Choose a better name for it! chmod and install it
好的,好的:为它选择一个更好的名字!chmod 并安装它
回答by d0wn
I had a case in which a had to write (INSERT) into DB the field that had such a bash script (on Windows's version of Intellij Idea) and then I had to read out the script from DB back on Unix server.
On unix I got this subject's error $'do\r''
for the reason explained above.
我有一个案例,其中必须将具有此类 bash 脚本的字段(在 Windows 版本的 Intellij Idea 上)写入(插入)到 DB 中,然后我不得不在 Unix 服务器上从 DB 中读出该脚本。$'do\r''
由于上述原因,在 unix 上我得到了这个主题的错误。
The solution was - copy\paste script on Win into Notepad, change in Notepad unix line endings from Windows (CR LF) to Unix (LF), save it in a file. Open file, copy\paste into Idea's database console. Then it worked fine on Unix for the case it was not origin from Unix.
解决方案是 - 将 Win 上的脚本复制\粘贴到记事本中,将记事本 unix 行尾从 Windows (CR LF) 更改为 Unix (LF),将其保存在一个文件中。打开文件,复制\粘贴到 Idea 的数据库控制台中。然后它在 Unix 上运行良好,因为它不是来自 Unix。
(Idea also has options to choose line endings in its editor)
(Idea 还提供了在其编辑器中选择行尾的选项)