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
Sed gives: sed: can't read : No such file or directory
提问by Bas van Dijk
I have the following bash
script 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
脚本,它为找到的每个图像重复。它需要遍历所有html、css和js文件,并替换该文件中所有出现的图像。
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 sed
gives:
但是,当我运行脚本时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 sed
command 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
?
-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.
-i
表示就地,即直接在文件中编辑。-i ''
表示就地编辑名称为空字符串的文件。
由于可能没有名称为空字符串的文件,sed 抱怨它无法读取它。
Note 1 platform dependency:
The syntax of -i
is one difference between GNU sed and sed from mac os.
注 1平台依赖性:
的语法-i
是 GNU sed 和 mac os sed 之间的一个区别。
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 allowssed -i filename -e "expression" AnotherFileName
which is an unintentionally camouflaged version ofsed -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 -i
argument 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