gpt4 book ai didi

bash - sed 在 bash 脚本中不起作用

转载 作者:行者123 更新时间:2023-11-29 09:06:31 26 4
gpt4 key购买 nike

我已经搜索了几个小时来寻找这个问题的答案,这个问题看起来简单得令人沮丧......

我有一个 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

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

关于bash - sed 在 bash 脚本中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6171291/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com