Reputation: 2818
I have bash script for teamcity.It puts build variables value to file using placeholders.
sed -i ""
-e "s/BUNDLE_VERSION/%bundle_version%/"
-e "s/BUNDLE_ID/%bundle_id%/"
-e "s/APP_TITLE/%title%/" "%system.teamcity.build.tempDir%/info.plist"
%var% - replaced to value of var before run bash script by teamcity.
It doesn't work with variables which have '
, "
, /
and probably other non-alphanumeric symbols.
For example with title = Auto Shop "SuperCar"
it crashes
[12:22:07]sed: 1: ""s/APP_TITLE/Auto ...": invalid command code "
How to fix script to work with non-alphanumeric symbols?
Upvotes: 1
Views: 233
Reputation: 8769
Before passing it to sed
escape the special/meta characters like this:
BUNDLE_VERSION=$(echo "$BUNDLE_VERSION" | sed -r 's/\\/\\\\/g;s/\//\\\//g;s/\^/\\^/g;s/\[/\\[/g;s/'\''/'\'"\\\\"\'\''/g;s/\]/\\]/g;s/\*/\\*/g;s/\$/\\$/g;s/\./\\./g')
Example:
$ echo "^[a/b'c\d]*$." | sed -r 's/\\/\\\\/g;s/\//\\\//g;s/\^/\\^/g;s/\[/\\[/g;s/'\''/'\'"\\\\"\'\''/g;s/\]/\\]/g;s/\*/\\*/g;s/\$/\\$/g;s/\./\\./g'
\^\[a\/b'\''c\\d\]\*\$\.
and use single quotes while putting in variables for the main expression. Its better to use some other tool for passing variables like awk
's -v
option.
Upvotes: 1