0

This question is related to post: Setting Variables Value in a File in Different Directory Using Loops and sed Bash Script

I'm looking for a way to check variable value in file and update it as needed. To change the value I do the following:

#!/bin/bash

cd '/home/me/'
source server.properties
server_var="$server_name"
echo "$server_var"
echo "$application_name"
if [ "$server_var" != "server1" ]; then
    echo "Uncorrect Value"
    sed -i -e 's@server_name=$server_var@server_name=server1@g' server.properties
fi
cd '/home/test/'

Thank you for the suggestion, now my if statement work, but the sed command doesnt update the variable value in the file. Can you please point me in the right direction regarding this issue?

Thank you,

1 Answer 1

0
if [ -n "$server_name" ]
then
    sed ...
fi

BTW, I don't think your set command can set $server_name. It will set $1, $2, etc.

Sign up to request clarification or add additional context in comments.

4 Comments

thank you for the answer. I followed the discussion here: stackoverflow.com/questions/2652753/… regarding the set -- $(<server.properties) but it doesn't seem to work. Do you mind explaining what's the condition in [ -n "$server_name"]?
source server.properties seems to do the job.
Actually, I was missing "" instead of ''
If you look in that other question, you'll see that the answer uses $1 and $2, just as I said. RTFM.

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.