如何以优化方式用多行替换exec文件中的字符串 [英] How to replace the string in exec file with multi line in optimized manner
问题描述
下面提供的是可执行文件.通过脚本,我需要以优化的方式终结该脚本.
Provided below is executable file. through script i need to endpoints to it in optimized manner.
文件名:taoexec
filename : taoexec
#!/bin/bash
. /etc/init.d/functions
PIDFILE=/var/run/Naming_Service.pid
PORT=
OPTIONS="-p ${PIDFILE}"
RETVAL=0
prog="Naming_Service"
start() {
echo -n $"Starting $prog: "
if [ $UID -ne 0 ]; then
RETVAL=1
failure
else
setsid /usr/local/bin/Naming_Service ${OPTIONS} &
RETVAL=$?
fi
echo
return $RETVAL
}
stop() {
echo -n $"Stopping $prog: "
if [ $UID -ne 0 ]; then
RETVAL=1
failure
else
killproc /usr/local/bin/Naming_Service
RETVAL=$?
fi
echo
return $RETVAL
}
restart(){
stop
start
}
case "$1" in
start) start ;;
stop) stop ;;
restart) restart ;;
status) status -p ${PIDFILE} ${prog}; RETVAL=$? ;;
*) echo "Usage: $0 {start|stop|status|restart}"; RETVAL=1
esac
来自环境变量,我正在读取端口号,并尝试将端口和主机名替换为该文件.当我使用下面提到的脚本时,没有实现.让我知道我如何实现这一目标
from Environment variables I'm reading the port numbers and I try to replace port and hostname into that file. While i'm using below mentioned script ,not achieved. let me know how i can achieve this
NS_HOSTNAME=$(grep "`hostname`" /etc/hosts|awk '{print $1}')
namingService_ports=$NS_PORTS
echo $namingService_ports
IFS=','
read -r -a portArray <<< "$namingService_ports"
for num in "${portArray[@]}";
do
sed 's~^\(OPTIONS\)="\(-p \${PIDFILE\)}~\1'"_$num"'="\2'"_$num"'} -ORBEndpoint iiop://'"$NS_HOSTNAME"':'"$num"'~' <<< 'OPTIONS="-p ${PIDFILE}"' >> "/etc/init.d/tao"
done
如果我提供NS_PORTS = 13021,13022,13023
I'm expecting the Following output at last, if I provided NS_PORTS=13021,13022,13023
#!/bin/bash
. /etc/init.d/functions
PIDFILE_13021=/var/run/Naming_Service_13021.pid
PIDFILE_13022=/var/run/Naming_Service_13022.pid
PIDFILE_13023=/var/run/Naming_Service_13023.pid
PORT=
OPTIONS_13021="-p ${PIDFILE_13021} -ORBEndpoint iiop://10.12.23.34:13021"
OPTIONS_13022="-p ${PIDFILE_13022} -ORBEndpoint iiop://10.12.23.34:13022"
OPTIONS_13023="-p ${PIDFILE_13023} -ORBEndpoint iiop://10.12.23.34:13023"
start() {
echo -n $"Starting $prog: "
if [ $UID -ne 0 ]; then
RETVAL=1
failure
else
setsid /usr/local/bin/Naming_Service ${OPTIONS_13021} &
setsid /usr/local/bin/Naming_Service ${OPTIONS_13022} &
setsid /usr/local/bin/Naming_Service ${OPTIONS_13023} &
RETVAL=$?
fi
echo
return $RETVAL
}
stop() {
echo -n $"Stopping $prog: "
if [ $UID -ne 0 ]; then
RETVAL=1
failure
else
killproc /usr/local/bin/Naming_Service
RETVAL=$?
fi
echo
return $RETVAL
}
restart(){
stop
start
}
case "$1" in
start) start ;;
stop) stop ;;
restart) restart ;;
status) status -p ${PIDFILE} ${prog}; RETVAL=$? ;;
*) echo "Usage: $0 {start|stop|status|restart}"; RETVAL=1
esac
任何人都可以为此提供解决方案.
Anybody can give the solution for this.
推荐答案
是否有任何原因不能使脚本每次运行都从头开始生成文件?
Is there any reason you can't just generate the file from scratch each time the script runs?
#!/bin/bash
FILENAME=/etc/init.d/tao
NS_HOSTNAME=$(grep "`hostname`" /etc/hosts|awk '{print $1}')
namingService_ports=$NS_PORTS
echo $namingService_ports
cat > "$FILENAME" << EOF
#!/bin/bash
. /etc/init.d/functions
PORT=
EOF
IFS=','
read -r -a portArray <<< "$namingService_ports"
for num in "${portArray[@]}" ; do
echo PIDFILE_$num=/var/run/Naming_Service_$num.pid
echo OPTIONS_$num=\"-p \${PIDFILE_$num} -ORBEndpoint iiop://${NS_HOSTNAME}:$num\"
done >> "$FILENAME"
这篇关于如何以优化方式用多行替换exec文件中的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!