cygwin 中的简单 shell 脚本

发布于 2024-08-04 00:06:22 字数 573 浏览 2 评论 0原文

#!/bin/bash
echo 'first line' >foo.xml
echo 'second line' >>foo.xml

我是 shell 脚本的新手。

我正在尝试在 cygwin 中运行上面的脚本。我希望能够将一行接着另一行写入一个新文件。

但是,当我执行上面的脚本时,我在 foo.xml 中看到以下内容:

second line

第二次运行脚本时,我在 foo.xml 中看到:

second line
second line

等等。

另外,运行脚本后,我看到命令提示符处显示以下错误:

: No such file or directory.xml

我最终将在 unix 机器上运行此脚本,我只是尝试使用 cygwin 来开发它。因此,如果您能指出这是否是 cygwin 的奇怪之处,如果是,我是否应该避免尝试使用 cygwin 来开发此类脚本,我将不胜感激。

提前致谢。

#!/bin/bash
echo 'first line' >foo.xml
echo 'second line' >>foo.xml

I am a total newbie to shell scripting.

I am trying to run the above script in cygwin. I want to be able to write one line after the other to a new file.

However, when I execute the above script, I see the follwoing contents in foo.xml:

second line

The second time I run the script, I see in foo.xml:

second line
second line

and so on.

Also, I see the following error displayed at the command prompt after running the script:

: No such file or directory.xml

I will eventually be running this script on a unix box, I am just trying to develop it using cygwin. So I would appreciate it if you could point out if it is a cygwin oddity and if so, should I avoid trying to use cygwin for development of such scripts?

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

猫性小仙女 2024-08-11 00:06:22

在 shell 脚本上运行 dos2unix。这将解决问题。

Run dos2unix on your shell script. That will fix the problem.

2024-08-11 00:06:22

我遇到了与原始海报相同的问题:一个非常简单的脚本文件在 Cygwin 中无法工作。

感谢唐·布兰森提供的线索。
对我来说的修复已内置到我正在使用的文本编辑器中。 (大多数程序员的编辑器都有这样的功能。)例如,在我的例子中,我使用的是 Notepad++,它有一个菜单项可以将文件行结尾转换为 Unix 风格。从菜单中:[编辑]->[EOL 转换]->[Unix (LF)]

然后脚本的行为符合预期。

I had the same kind of problem as the original poster: A very simple script file was not working in Cygwin.

Thanks to Don Branson for the clue.
The fix for me was built into the text editor I'm using. (Most programmer's editors have a feature like this.) For example, in my case I'm using Notepad++, which has a menu item to convert the file line endings to Unix-style. From the menu: [Edit]->[EOL Conversion]->[Unix (LF)]

Then the script behaved as expected.

演多会厌 2024-08-11 00:06:22

但这里肯定还有其他问题。当我尝试时,它按预期工作。

<代码>> foo.xml 将该行放入 foo.xml 中,替换以前的所有内容。
<代码>>> foo.xml 附加到文件

But there must be something else that is wrong here. When I try it, it works as expected.

> foo.xml puts the line into foo.xml, replacing any previous contents.
>> foo.xml appends to file

弄潮 2024-08-11 00:06:22

听起来您可能在某个地方有错字。另请记住,虽然 Windows 命令提示符可以容忍包含嵌入空格的路径,但 cygwin 的 shell 不会,因此如果您有包含嵌入空格的文件名,则需要引用文件名或转义空格:

echo 'first line' > 'My File.txt'
echo 'first line' > My\ File.txt

相同适用于某些“特殊”字符,包括引号、与号 (&)、分号 (;) 以及除句号/句号 (.) 之外的大多数标点符号。

因此,如果您使用正在运行的确切脚本看到这些问题(即复制并粘贴它,不可能出现转录错误),那么可能会发生一些我无法解释的真正奇怪的事情。否则,某处可能存在放错位置的空格或未引用的字符。

It sounds like you may have a typo somewhere. Also keep in mind that while the Windows command prompt can be forgiving about paths with embedded spaces, cygwin's shells will not be, so if you have a filename that contains embedded spaces, you need to either quote the filename or escape the spaces:

echo 'first line' > 'My File.txt'
echo 'first line' > My\ File.txt

The same goes for certain "special" characters including quotes, ampersand (&), semicolons (;) and generally most punctuation other than period/full-stop (.).

So if you are seeing those issues using the exact script that you are running (i.e. you copy and pasted it, there is no possibility of transcription errors) then something truly strange may be happening that I can't explain. Otherwise, there may be a misplaced space or unquoted character somewhere.

一桥轻雨一伞开 2024-08-11 00:06:22

我无法重现你的结果。您引用的脚本看起来是正确的,并且确实按我在此处安装的 Cygwin 中的预期工作,生成包含行 first linesecond line 的文件 foo.xml;暗示您实际运行的内容与您引用的内容以某种方式导致问题不同。

该错误消息暗示第一 echo 行中的文件名存在某种问题。您正在运行的脚本中是否有一些不可打印的字符?您是否错过了文件名中的空格转义?您是否替换了 shell 变量并错误输入了变量名称或未能转义生成的字符串?

I cannot reproduce your results. The script you quote looks correct, and indeed works as expected in my installation of Cygwin here, producing the file foo.xml containing the lines first line and second line; implying that what you are actually running differs from what you quoted in some way that is causing the problem.

The error message implies some sort of problem with the filename in the first echo line. Do you have some nonprintable characters in the script you are running? Have you missed escaping a space in the filename? Are you subsituting shell variables and mistyping the name of the variable or failing to escape the resulting string?

不念旧人 2024-08-11 00:06:22

以上应该可以正常工作..
但是,您始终可以指定一个定界符:

#!/bin/bash
cat <<EOF > foo.xml
first line
second line
EOF

The above should work normally..
However you can always specify a heredoc:

#!/bin/bash
cat <<EOF > foo.xml
first line
second line
EOF
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文