bash sed 给出:sed:无法读取:没有这样的文件或目录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43171648/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 22:30:58  来源:igfitidea点击:

Sed gives: sed: can't read : No such file or directory

bashsedquotinggnu-sed

提问by Bas van Dijk

I have the following bashscript which repeats for each image found. It needs to iterated over all html, cssand jsfiles, and replace all occurrences of an image within that file.

我有以下bash脚本,它为找到的每个图像重复。它需要遍历所有htmlcssjs文件,并替换该文件中所有出现的图像。

 for image in app/www/images-theme-dark/*.png
    do
        echo "installing icon" $image

        # extract filename from iconpath
        iconfile=$(basename $image)
        iconPath="images/"$(basename $image)

        # replace paths in all files containing icon paths
        find app/www -type f \( -name "*.html" -or -name "*.css" -or -name "*.js" \
                            -or -name "*.appcache" \)  \
            -exec sed -i '' -e 's|$iconPath|images-theme-dark/$iconfile|g' "{}" \;

    done

However when I run the script sedgives:

但是,当我运行脚本时sed

sed: can't read : No such file or directory

sed: can't read : No such file or directory

On StackOverflow I've found sed: can't read : No such file or directoryBut I already had quotes around {}

在 StackOverflow 上,我发现sed: can't read : No such file or directory但我已经有了引号{}

When I echo the sedcommand and and execute it on the command line manually there is no error.

当我回显sed命令并在命令行上手动执行它时,没有错误。

I am using GNU sed v4.2.2 on Raspbian GNU/Linux 8.0 (jessie)

我在 Raspbian GNU/Linux 8.0 (jessie) 上使用 GNU sed v4.2.2

Does someone see what could be wrong here?

有人看到这里有什么问题吗?

回答by Yunnosch

(Compiling an answer from comments, the know-how is by melpomene and AlexP.)

从评论中编译答案,诀窍是 melpomene 和 AlexP。

What is that ''after sed -i?

''之后是什么sed -i

-imeans in-place, that is, edit in the file directly.
-i ''means edit in place a file whose name is the empty string.
Since there probably is no file whose name is the empty string, sed complains that it cannot read it.

-i表示就地,即直接在文件中编辑。
-i ''表示就地编辑名称为空字符串的文件。
由于可能没有名称为空字符串的文件,sed 抱怨它无法读取它。

Note 1 platform dependency:
The syntax of -iis one difference between GNU sed and sed from mac os.

注 1平台依赖性
的语法-i是 GNU sed 和 mac os sed 之间的一个区别。

Note 2 "usual" order of arguments:
The -eswitch to indicate the sed code allows having it in between file names.
This is a trap (in which I for example got caught embarassingly), by making you trip over your expectations of what you find where in an sed command line.
It allows
sed -i filename -e "expression" AnotherFileName
which is an unintentionally camouflaged version of
sed -i'NoExtensionGiven' "expression" filename AnotherFileName.

注意 2参数的“通常”顺序:指示 sed 代码
-e开关允许将其置于文件名之间。
这是一个陷阱(例如,我在其中尴尬地陷入了困境),它使您超出对在 sed 命令行中的位置的期望。
它允许
sed -i filename -e "expression" AnotherFileName
哪个是
sed -i'NoExtensionGiven' "expression" filename AnotherFileName.

回答by Acidic9

For support on both OSX and Linux, I use a simple if check to see if the bash script is running on OSX or Linux, and adjust the command's -iargument based on that.

为了支持 OSX 和 Linux,我使用了一个简单的 if 检查来查看 bash 脚本是在 OSX 还是 Linux 上运行,并-i根据它调整命令的参数。

if [[ "$OSTYPE" == "darwin"* ]]; then
  sed -i '' -e 's|$iconPath|images-theme-dark/$iconfile|g' "{}"
else
  sed -i -e 's|$iconPath|images-theme-dark/$iconfile|g' "{}"
fi