sed在bash脚本中不起作用 [英] sed doesn't work from within bash script
问题描述
我已经花了几个小时寻找这个问题的答案,这似乎很简单...
I've searched for hours looking for the answer to this question which seems frustratingly simple...
我有一个bash脚本,我对其进行了简化,以查找导致其停止工作的行,并留下以下内容:
I have a bash script which I've simplified to find the line that's stopping it from working and am left with:
#!/bin/bash
#
sed -i -e "s/<link>/\n/g" /usb/lenny/rss/tmp/rss.tmp
如果我运行此脚本,则rss.tmp文件没有任何反应-但是,如果我从终端调用此完全相同的sed命令,它将按预期进行所有替换.
If I run this script, nothing happens to the file rss.tmp - but if I call this exact same sed command from the terminal, it makes all the replacements as expected.
有人知道我在做什么错吗?
Anyone have any idea what I'm doing wrong here?
推荐答案
根据讨论,该问题听起来像是Cygwin Shell问题.问题是shell脚本可能没有\ r \ n行终止-它们需要\ n终止.cygwin的早期版本的行为有所不同.Cygwin常见问题解答中的相关部分,位于 http://cs.nyu.edu/〜yap/prog/cygwin/FAQs.html
Based on the discussion the issue sounds like it is a cygwin shell problem. The issue is that shell scripts may not have \r\n line terminations - they need \n terminations. Earlier versions of cygwin behaved differently. The relevant section from a Cygwin FAQ at 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
这篇关于sed在bash脚本中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!