如何以优化的方式用多行替换exec文件中的字符串

时间:2016-11-24 16:15:57

标签: linux bash shell sed

下面提供的是可执行文件。通过脚本我需要以优化的方式对它进行端点化。

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

来自环境变量我正在读取端口号,我尝试将端口和主机名替换为该文件。虽然我使用下面提到的脚本,但没有实现。让我知道如何实现这个目标

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

,我最后会期待以下输出
    #!/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

任何人都可以为此提供解决方案。

1 个答案:

答案 0 :(得分:0)

是否有任何理由不能在每次脚本运行时从头开始生成文件?

#!/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"