將/ 替換成 \/ sed "s/\//\\\\\//" .
將/ 替換成 \/ sed "s/\//\\\\\//" .
sed "s/\//\\\\\//"
將/ 替換成 \/
完整shell如下 為不同前端製作service
realadd.sh
view plaincopy to clipboardprint?01.#!/bin/bash
02.#replace service path and move to /etc/init.d
03.#need template nginxservice,php-fpmservice,apacheservice
04.
05.nginx=$(lsof | grep txt | grep nginx | awk '{print $NF}' | sort | uniq | grep nginx);
06.echo $nginx
07.if [ ! -z $nginx ] ; then
08. #addslashes / => \/
09. nginx=$(echo ${nginx} | sed "s/\//\\\\\//g");
10. sed "s/NGINXSERVICE/${nginx}/" nginxservice > nginx
11. chmod +x nginx
12. mv nginx /etc/init.d/
13.fi
14.
15.phpfpm=$(lsof |grep txt | grep php-fpm | awk '{print $NF}' | sort |uniq | grep php-fpm);
16.echo $phpfpm
17.if [ ! -z $phpfpm ] ; then
18. #addslashes / => \/
19. phpfpm=$(echo ${phpfpm} | sed "s/\//\\\\\//g");
20. sed "s/PHPFPMSERVICE/${phpfpm}/" php-fpmservice > php-fpm
21. chmod +x php-fpm
22. mv php-fpm /etc/init.d/
23.fi
24.
25.apache=$(lsof |grep txt | grep apache | awk '{print $NF}' | sort |uniq | grep apache);
26.apache=$(echo "/opt/app/apache2/bin/httpd" | awk -F"/bin/httpd" '{print $1}')/bin/apachectl
27.echo $apache
28.if [ ! -z $apache ] ; then
29. #addslashes / => \/
30. apache=$(echo ${apache} | sed "s/\//\\\\\//g");
31. sed "s/APACHESERVICE/${apache}/" apacheservice > apache
32. chmod +x apache
33. mv apache /etc/init.d/
34.fi
#!/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/
finginxservice
view plaincopy to clipboardprint?01.#!/bin/bash
02.#/etc/init.d/nginx
03.
04.# Source function library.
05.if [ -f /etc/init.d/functions ] ; then
06. . /etc/init.d/functions
07.elif [ -f /etc/rc.d/init.d/functions ] ; then
08. . /etc/rc.d/init.d/functions
09.else
10. exit 1
11.fi
12.
13.#config
14.service='NGINXSERVICE'
15.service_name='nginx'
16.
17.#functions
18.start()
19.{
20. echo 'start service'
21. echo "${service}"
22. ${service}
23. if [ "$?" -eq 0 ] ; then
24. success
25. else
26. failure
27. fi
28.}
29.stop()
30.{
31. echo 'stop service'
32. echo "killproc ${service_name}"
33. killproc ${service_name}
34.}
35.reload()
36.{
37. echo 'reload service'
38. echo "${service} -s reload"
39. ${service} -s reload
40. if [ "$?" -eq 0 ] ; then
41. success
42. else
43. failure
44. fi
45.}
46.test()
47.{
48. echo 'test service'
49. echo "${service} -t"
50. ${service} -t
51.}
52.
53.
54.case "$1" in
55.start)
56. start
57. ;;
58.stop)
59. stop
60. ;;
61.reload)
62. reload
63. ;;
64.restart)
65. stop
66. start
67. ;;
68.test)
69. test
70. ;;
71.status)
72. status ${service_name}
73. ;;
74.*)
75. echo "usage: $0 start|stop|reload|restart|test|status"
76. exit 0;
77.esac
《解決方案》
/ => \/ 那麼多的\\?難道sed跟perl不同?
s/\//\\\//
這樣還不行?為什麼啊
《解決方案》
回復 2# lewphee
#echo a/b/c | sed 's/\//\\\//g'
a\/b\/c