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

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

问题描述

我有以下bash脚本,该脚本针对找到的每个图像重复执行.它需要遍历所有 html css js 文件,并替换该文件中所有出现的图像.

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 filename -e "expression" AnotherFileName
这是
的无意伪装版本 sed -i'NoExtensionGiven' "expression" filename AnotherFileName.

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天全站免登陆