How to replace the string in exec file with multi

2019-09-15 15:57发布

Provided below is executable file. through script i need to endpoints to it in optimized manner.

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

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.

1条回答
Anthone
2楼-- · 2019-09-15 16:28

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"
查看更多
登录 后发表回答