2

如何使用给定文件中的适当值更新特定条目,

假设我有一个文件名 hello.conf,

/agent/process="starthelloJava",start="/opt/starthello.sh",start_dir="/opt/hello",start_args=""
/agent/process="stophelloJava",stop="/opt/stophello.sh",stop_dir=/opt/hello",stop_args="",stop_timeout="30"

更新starthelloJava

start_args="-XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/var/"

输出

/agent/process="starthelloJava",start="/opt/starthello.sh",start_dir="/opt/hello",start_args="-XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/var/"

清空start_args进程starthelloJava

/agent/process="starthelloJava",start="/opt/starthello.sh",start_dir="/opt/hello",start_args=""
/agent/process="stophelloJava",stop="/opt/stophello.sh",stop_dir=/opt/hello",stop_args="",stop_timeout="30"

我正在做的方式很长,我正在根据输入重写整个文件。

这是我的方法,效率不高

cp hello.conf hello.conf_bak
remove entry first
cat /opt/CSCObac/agent/conf/agent.conf_bak | grep -v /opt/starthello.sh  > hello.conf
echo "/agent/process=\"starthelloJava\",start=\"/opt/starthello.sh\",start_dir=\"/opt/hello\",start_args=\"-XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/var/\" >> hello.conf
4

1 回答 1

1
set -- '-XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/var/'
sed -i -r "s!(.*starthelloJava.*=).*!\1\"$1\"!" hello.conf

或清空值

set -- ''
sed -i -r "s!(.*starthelloJava.*=).*!\1\"$1\"!" hello.conf
于 2013-02-05T17:20:58.273 回答