sed 给出:sed:无法读取:没有这样的文件或目录 [英] Sed gives: sed: can't read : No such file or directory

查看:61
本文介绍了sed 给出:sed:无法读取:没有这样的文件或目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have the following bash script which repeats for each image found. It needs to iterated over all html, css and js files, and replace all occurrences of an image within that file.

 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

但是当我运行脚本 sed 给出:

However when I run the script sed gives:

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

在 StackOverflow 上,我发现 sed:无法阅读:没有这样的文件或目录 但我已经在 {}

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

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

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

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

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

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

Does someone see what could be wrong here?

推荐答案

(从评论中编译答案,诀窍是由 melpomene 和 AlexP 提供的.)

sed -i 后面的 '' 是什么?

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

-i means 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.

注意 1 平台依赖性:
-i 的语法是 GNU sed 和 mac os 中的 sed 的一个区别.

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

注意 2 参数的通常"顺序:
-e 开关指示 sed 代码允许在文件名之间使用它.
这是一个陷阱(例如,我在其中被尴尬地抓住了),它使您超出了对在 sed 命令行中的位置的期望.
它允许
sed -i 文件名 -e "表达式" 另一个文件名
这是
无意伪装的版本sed -i'NoExtensionGiven'表达式"文件名另一个文件名.

Note 2 "usual" order of arguments:
The -e switch 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.

这篇关于sed 给出:sed:无法读取:没有这样的文件或目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆