sed "s/\//\\\\\//"
将/ 替换成 \/
完整shell如下 为不同前端制作service
realadd.sh
#!/bin/bash
#replace service path and move to /etc/init.d
#need template nginxservice,php-fpmservice,apacheservice
nginx=$(lsof | grep txt | grep nginx | awk '{print $NF}' | sort | uniq | grep nginx);
echo $nginx
if [ ! -z $nginx ] ; then
#addslashes / => \/
nginx=$(echo ${nginx} | sed "s/\//\\\\\//g");
sed "s/NGINXSERVICE/${nginx}/" nginxservice > nginx
chmod +x nginx
mv nginx /etc/init.d/
fi
phpfpm=$(lsof |grep txt | grep php-fpm | awk '{print $NF}' | sort |uniq | grep php-fpm);
echo $phpfpm
if [ ! -z $phpfpm ] ; then
#addslashes / => \/
phpfpm=$(echo ${phpfpm} | sed "s/\//\\\\\//g");
sed "s/PHPFPMSERVICE/${phpfpm}/" php-fpmservice > php-fpm
chmod +x php-fpm
mv php-fpm /etc/init.d/
fi
apache=$(lsof |grep txt | grep apache | awk '{print $NF}' | sort |uniq | grep apache);
apache=$(echo "/opt/app/apache2/bin/httpd" | awk -F"/bin/httpd" '{print $1}')/bin/apachectl
echo $apache
if [ ! -z $apache ] ; then
#addslashes / => \/
apache=$(echo ${apache} | sed "s/\//\\\\\//g");
sed "s/APACHESERVICE/${apache}/" apacheservice > apache
chmod +x apache
mv apache /etc/init.d/
fi
nginxservice
#!/bin/bash
#/etc/init.d/nginx
# Source function library.
if [ -f /etc/init.d/functions ] ; then
. /etc/init.d/functions
elif [ -f /etc/rc.d/init.d/functions ] ; then
. /etc/rc.d/init.d/functions
else
exit 1
fi
#config
service='NGINXSERVICE'
service_name='nginx'
#functions
start()
{
echo 'start service'
echo "${service}"
${service}
if [ "$?" -eq 0 ] ; then
success
else
failure
fi
}
stop()
{
echo 'stop service'
echo "killproc ${service_name}"
killproc ${service_name}
}
reload()
{
echo 'reload service'
echo "${service} -s reload"
${service} -s reload
if [ "$?" -eq 0 ] ; then
success
else
failure
fi
}
test()
{
echo 'test service'
echo "${service} -t"
${service} -t
}
case "$1" in
start)
start
;;
stop)
stop
;;
reload)
reload
;;
restart)
stop
start
;;
test)
test
;;
status)
status ${service_name}
;;
*)
echo "usage: $0 start|stop|reload|restart|test|status"
exit 0;
esac
附:service编写方法 http://blog.csdn.net/sunvince/article/details/6975396