Reputation: 17694
I have a file called foo.sh
how can I edit /append the following value:
-Dhttp.proxyHost=<yourProxyHost> -Dhttp.proxyPort=<yourProxyPort>
to the file with contents of the AMBARI_JVM_ARGS
foo
bar baz
export AMBARI_JVM_ARGS="$AMBARI_JVM_ARGS -Xms512m -Xmx2048m -XX:MaxPermSize=128m -Djava.security.auth.login.config=$ROOT/etc/ambari-server/conf/krb5JAASLogin.conf -Djava.security.krb5.conf=/etc/krb5.conf -Djavax.security.auth.useSubjectCredsOnly=false"
export PATH=$PATH:$ROOT/var/lib/ambari-server
some
more things
so that the desired result would look like:
foo
bar baz
export AMBARI_JVM_ARGS="$AMBARI_JVM_ARGS -Xms512m -Xmx2048m -XX:MaxPermSize=128m -Djava.security.auth.login.config=$ROOT/etc/ambari-server/conf/krb5JAASLogin.conf -Djava.security.krb5.conf=/etc/krb5.conf -Djavax.security.auth.useSubjectCredsOnly=false -Dhttp.proxyHost=<yourProxyHost> -Dhttp.proxyPort=<yourProxyPort>"
export PATH=$PATH:$ROOT/var/lib/ambari-server
some
more things
So far, I could not get it to work with sed.
Upvotes: 0
Views: 304
Reputation: 3840
You need regular expression replacement, like this:
sed -ri.bak 's/^(export AMBARI_JVM_ARGS=.*)"$/\1 -Dhttp.proxyHost=<yourProxyHost> -Dhttp.proxyPort=<yourProxyPort>"/g' foo.sh
The '-r' enables regular-expression extend. '-i' means 'edit in-place' which is what you need: change the file. If the '.bak' is specified, sed creates 'foo.sh.bak' to save the file before changed, you can ignore the '.bak' (but keep '-i') then the backup file will not be created.
Upvotes: 1
Reputation: 189658
Your code is already appending text to the previous value of the variable you are trying to modify. Assuming the order of the options isn't significant, simply call the script with the value already appended to the variable.
AMBARI_JVM_ARGS="$AMBARI_JVM_ARGS -Dhttp.proxyHost=<yourProxyHost> -Dhttp.proxyPort=<yourProxyPort>" ./foo.sh
(This turns out very long, so here's a shortened pseudocode version for legibility:
var="$var with new value appended" ./foo.sh
i.e. override the value of var
with a value which includes the previous value and a suffix, and run the script with this temporary value in place for the duration of the command. The shell command var=value command
sets var
to value
, runs command
, and restores the original value of var
.)
Editing the source of the script seems like very poor practice. The proper solution is almost always to refactor the script to take a parameter instead. But yours already does that, so no refactoring is necessary -- just use the facility which is already there.
Upvotes: 1
Reputation: 95978
If you want to append at the end of the line that begins with "export", you can do:
sed -i -E "s/export\(.*\)$/export\1<TEXT_TO_APPEND>/" AMBARI_JVM_ARGS
Upvotes: 0