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

BashSedQuotingGnu Sed

Bash Problem Overview


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

However when I run the script sed gives:

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

On StackOverflow I've found https://stackoverflow.com/questions/22716187/sed-cant-read-no-such-file-or-directory But I already had quotes around {}

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

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

Does someone see what could be wrong here?

Bash Solutions


Solution 1 - Bash

(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.

Solution 2 - Bash

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

Solution 3 - Bash

In my bash scripts I use something like that (to support both MacOS and Linux distributions):

SEDOPTION=
if [[ "$OSTYPE" == "darwin"* ]]; then
  SEDOPTION="-i ''"
fi

sed $SEDOPTION "/^*/d" ./file

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBas van DijkView Question on Stackoverflow
Solution 1 - BashYunnoschView Answer on Stackoverflow
Solution 2 - BashAri SeyhunView Answer on Stackoverflow
Solution 3 - BashstorenthView Answer on Stackoverflow