user4017080
user4017080

Reputation:

Sed not working in usual way - Shell scripting

I have two sed command which includes in my cook.sh script. One command is

sed -E -i "s/^(\\\$mainDomain=\")[^\"]+(\";)$/\1$MainDomain\2/" /var/config.php

This is working fine.

But the below command which is almost same. But it is not working.

sed -E -i "s/^(\\\$authURI=\")[^\"]+(\";)$/\1$duo_auth\2/" /var/config.php

That give the below error message

sed: -e expression #1, char 36: unknown option to `s'

Any idea on this ?

Upvotes: 0

Views: 81

Answers (2)

Kusalananda
Kusalananda

Reputation: 15633

The problem is probably due to $duo_auth containing an unescaped /. This means that the sed editing script will have a syntax error.

You may pick any other character to use as a delimiter in the s/.../.../ command, for example #:

sed "s#....#....#"

Just make sure that you pick a character that is not ever going to turn up in either $duo_auth or $authURI.

While testing, I'd recommend that you avoid using in-place-editing (-i) with sed. Also, the -i switch is horribly non-portable between sed implementations (some requires an argument).

Instead, do the slightly more cumbersome

sed -e "s#...#...#" data.in >data.in.tmp && mv -f data.in.tmp data.in

While testing, check the data.in.tmp file before moving it.

Upvotes: 0

Inian
Inian

Reputation: 85800

The issue is likely due to your replacement variable $duo_auth having a un-escaped /, change the default sed separator from / to ~ as

sed -E -i "s~^(\\\$authURI=\")[^\"]+(\";)$~\1$duo_auth\2~" /var/config.php

Try it without -i for seeing if the replacement is as expected and put it back after confirmation.

Example:-

cat /var/config.php
<?php
$authURI="dev.digin.io";

now setting the variable

duo_auth="http://auth.uri.digin.io:3048/"

Now the replacement, without -i

sed -E "s~^(\\\$authURI=\")[^\"]+(\";)$~\1$duo_auth\2~" /var/config.php
<?php
$authURI="http://auth.uri.digin.io:3048/";

Upvotes: 1

Related Questions