0

This is my simple shell script

sample.sh

LOCALCONFIGDIR="Install"
CONFLOC="$LOCALCONFIGDIR/server.conf"
echo "Enter Web Url:"
read  weburl
echo "sed 's/^\(ServerName\)$/\1 "$weburl"/' "$CONFLOC
sed "'s/^\(ServerName\)$/\1 "$weburl"/' "$CONFLOC

When I run this code, I get the result in echo command as following.

sed 's/^\(ServerName\)$/\1 www.weburl.com/' Install/server.conf

But when executing sed command in the next line, It says the below error.

sed: -e expression #1, char 1: unknown command: `''

I tried the command produced in echo statement from Terminal screen, It is working. But Line number 5, doesn't working from shell script

0

1 Answer 1

1

You need to use one set of quotes, not two, and since you want the $weburl variable expanded, you need to use double quotes:

sed "s/^\(ServerName\)$/\1 $weburl/" "$CONFLOC"

That'll be OK as long as $weburl doesn't contain any slashes. If it does, you need to use a different character than /, such as %, to separate the parts of the substitute command:

sed "s%^\(ServerName\)$%\1 $weburl%" "$CONFLOC"
Sign up to request clarification or add additional context in comments.

4 Comments

It works. sed "s%$^(ServerName)$%\1 www.weburl.com%" Install/server.conf . But It doesn't add the text www.weburl.com after the ServerName.
I just removed a stray $ in the second sed; if you'd used that, it would have stopped the match working. If that isn't the problem, then maybe your configuration file has spaces before or after ServerName on the configuration line, or maybe the file doesn't have a line containing just ServerName.
Perhaps sed commands are best served by using single quotes and exposing the double quoted shell variables i.e. sed 's/.../'"${var}"'/' file. The reason being that meta-characters unwittingly used in the sed command(s) my be interpolated by the shell.
@potong: What you wrote is what I'd usually do, but it isn't crucial this time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.