如何通过sed在bash中向foo.sh的AMBARI_JVM_ARGS追加代理参数
Got it, let's fix this properly. You need to insert the proxy parameters inside the quoted value of AMBARI_JVM_ARGS (not just tacked onto the end of the line) in your foo.sh file. Here are two straightforward methods:
Method 1: Manual Edit (Great for Quick, One-Off Changes)
- Open the file with your preferred text editor, for example:
nano foo.sh - Locate the line starting with
export AMBARI_JVM_ARGS=. It’ll look like this: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"
- Add your proxy arguments right before the closing double quote (
"), like so: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>" - Save and exit the editor (for nano: press
Ctrl+O, hit Enter, thenCtrl+X).
Method 2: Automated Replacement with sed (Perfect for Scripts/Bulk Changes)
If you want to make this change via the command line without opening the file, use sed to modify the line in place. Replace <yourProxyHost> and <yourProxyPort> with your actual proxy details, then run:
sed -i 's/\(export AMBARI_JVM_ARGS="[^"]*\)/\1 -Dhttp.proxyHost=<yourProxyHost> -Dhttp.proxyPort=<yourProxyPort>/' foo.sh
Quick breakdown of how this works:
- The regex
\(export AMBARI_JVM_ARGS="[^"]*\)matches the start of the line up to (but not including) the closing double quote. \1refers to the matched portion, and we append the proxy arguments to it—ensuring they’re placed correctly inside the quoted string.
Verify the Change
After using either method, double-check the file to confirm the arguments are in the right spot:
grep "AMBARI_JVM_ARGS" foo.sh
You should see the proxy parameters inside the quoted value, not floating outside at the end of the line.
内容的提问来源于stack exchange,提问作者Georg Heiler

