我已经搜索了几个小时来寻找这个问题的答案,这似乎简单得令人沮丧......
我有一个 bash 脚本,我对其进行了简化,以找到阻止其工作的行,并留下:
#!/bin/bash
#
sed -i -e "s/<link>/\n/g" /usb/lenny/rss/tmp/rss.tmp
如果我运行此脚本,文件 rss.tmp 不会发生任何变化 - 但如果我从终端调用完全相同的 sed 命令,它会按预期进行所有替换。
有人知道我在这里做错了什么吗?
根据讨论,该问题听起来像是 cygwin shell 问题。
问题是 shell 脚本可能没有 \r\n 行终止符 - 它们需要 \n 终止符。早期版本的 cygwin 的行为有所不同。
Cygwin 常见问题解答中的相关部分位于http://cs.nyu.edu/~yap/prog/cygwin/FAQs.html http://cs.nyu.edu/~yap/prog/cygwin/FAQs.html
Q: Mysterious errors in shell scripts, .bashrc, etc
A: You may get mysterious messages when bash reads
your .bashrc or .bash_profile, such as
"\r command not found"
(or similar). When you get rid of empty lines, the
complaints about "\r" disappears, but probably other
errors remain. What is going on?
The answer may lie in the fact that a text file (also
called ASCII file) can come in two formats:
in DOS format or in UNIX format.
Most editors can automatically detect the formats
and work properly in either format.
In the DOS format, a new line is represented by two characters:
CR (carriage return or ASCII code 13) and LF (line feed or ASCII code 15).
In the UNIX format, a new line is represented by only
one character, LF. When your .bashrc file is read,
bash thinks the extra character is the name of a command,
hence the error message.
In Cygwin or unix, you can convert a file INFILE in DOS format
to a file OUTFILE in Unix format by calling:
> tr -d '\15' OUTFILE
NOTE:
If you now compare the number of characters in INFILE and OUTFILE,
you will see that the latter has lost the correct
number of characters (i.e., the number of lines in INFILE):
> wc INFILE OUTFILE
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)