In terms of what went wrong:
jquery.min.js
contains @
and #
characters, so neither of these is safe as an unescaped sigil.
Your sed
operation on jquery.min.js
is escaping /
s, even if you're using @
s in the outer sed
instance.
To fix this, you might change
file_content=$(sed 's:/:\\/:g' jquery.js)
...to...
file_content=$(sed 's:@:\\@:g' jquery.js)
Also, if you didn't remove the first line with a comment with licensing information, the replacement file isn't actually only one line. Putting an unescaped newline in sed
command terminates it.
The easy answer is not to use sed
at all. Consider, for instance, gsub_literal
, as defined in BashFAQ #21:
gsub_literal() {
# STR cannot be empty
[[ $1 ]] || return
# string manip needed to escape '\'s, so awk doesn't expand '\n' and such
awk -v str="${1//\\/\\\\}" -v rep="${2//\\/\\\\}" '
# get the length of the search string
BEGIN {
len = length(str);
}
{
# empty the output string
out = "";
# continue looping while the search string is in the line
while (i = index($0, str)) {
# append everything up to the search string, and the replacement string
out = out substr($0, 1, i-1) rep;
# remove everything up to and including the first instance of the
# search string from the line
$0 = substr($0, i + len);
}
# append whatever is left
out = out $0;
print out;
}
'
}
In your use case, that might look like the following:
gsub_literal \
'<script type="text/javascript" src="<?php echo $jquery_path ?>jQuery.js' \
"$(<../my_files/jquery.js)" \
<index.php >index.php.out && mv index.php.out index.php