shell读取在Windows中创建的文件不正确 [英] shell reads file created in windows incorrectly
问题描述
这是我的shell脚本和属性文件
test.sh
#!/ bin / bash
pre>
source ./default.properties
echo $ app_name
echo$ app_name ==
default.properties
app_name = TestApplication
开始时,我创建
test.sh
和default.properties
文件在我的intellij的窗口。然后我复制到Linux并执行test.sh
。但是,我得到的结果:TestApplication
然后使用vim创建具有相同内容的新
== tApplication
default.properties
。
<我得到正确的结果TestApplication
pre>
TestApplication ==
使用diff命令,它们真的不同,但我没有找到差别在哪里
diff default.properties default.properties_2
1c1
< app_name = TestApplication
---
> app_name = TestApplication
在Windows上创建的文件中有一些不可见的字符,
解决方案是的,你有一个DOS回车在行的末尾,分配
appName
变量。这个控制字符使得光标移动回到大多数终端的行的开头。
好的抓住;大多数问问这里甚至不知道这是一个问题。
bash
标记维基提供了有关解决此问题的部分。 / p>
Unix上的许多工具都会显示
^ M
(control-M)或其八进制代码\015
;在十六进制中它的字符代码为0x0D,十进制为13.
在将来,如果你绝对需要使用Windows编辑器,用Unix行结尾保存,使用
ftp
ASCII模式(这些天不是流行或安全的选项),或者在Unix上的文件上运行dos2unix
转移它。Here is my shell script and properties file
test.sh
#!/bin/bash source ./default.properties echo $app_name echo "$app_name =="
default.properties
app_name=TestApplication
At beginning, I create the
test.sh
anddefault.properties
file in my intellij on Window. And then I copy then into Linux and executetest.sh
. However, I get the result:TestApplication ==tApplication
Then I create the new
default.properties
with the same content using vim. And I get correct resultTestApplication TestApplication ==
Using diff command, they are really different and but I didn't found where is the difference
diff default.properties default.properties_2 1c1 < app_name=TestApplication --- > app_name=TestApplication
Is there some invisible character in the file I created on windows, or some encode issues?
解决方案Yes, you have a DOS carriage return at the end of the line which assigns the
appName
variable. This control character causes the cursor to move back to the beginning of the line in most terminals.Good catch; most askers here don't even realize this is a problem. The
bash
tag wiki has a section about troubleshooting this issue.Many tools on Unix will show the problematic character as
^M
(control-M) or its octal code\015
; in hex it's character code 0x0D and in decimal it's 13.In the future, if you absolutely have to use a Windows editor, save with Unix line endings, transfer the file using
ftp
ASCII mode (not a popular or safe option these days), or rundos2unix
on the file on Unix after transferring it.这篇关于shell读取在Windows中创建的文件不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!