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

放肆的年华 提交于 2019-11-30 08:05:43

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

What is that '' after 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.

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

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.

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.

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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!