设为首页 收藏本站
查看: 1363|回复: 0

[经验分享] 在centos系统shell脚本中cat和重定向符号<<EOF结合使用的注意...

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2016-11-8 09:36:54 | 显示全部楼层 |阅读模式
在centos系统shell脚本中cat和重定向符号<<EOF结合使用的注意事项
在运维人员编写shell脚本中,有时会需要将一些内容直接放在到一个文件,比如在一个shell脚本中配置一些内容再生成一个shell脚本,此时可以使用到cat命令和重定向符号“<<”以及EOF的使用。但是,在shell脚本中使用重定向符号生成shell脚本时,会遇到一些问题,比如,内容中含有特殊符号"#","`","$"时,(如果以“#”开头,则需要加转义符“\”)重定向会忽略这些特殊符号,而导致生成的shell脚本无法运行,此时只需要在这些特殊符号前加转义符号“\”即可,如下是一个自动安装nginx的脚本:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#!/bin/sh
#the script is install nginx
#echo "add user mars"
#groupadd xiaoyao && useradd -g xiaoyao xiaoyao && sed -i 's/\#PermitRootLogin yes/PermitRootLogin no/g' /etc/ssh/sshd_config && service sshd restart && passwd xiaoyao
echo "configure sysctl"
mv /etc/sysctl.conf /etc/sysctl.conf.bak
cat >>/etc/sysctl.conf<<EOF
net.ipv4.ip_forward = 0
net.ipv4.conf.default.rp_filter = 1
net.ipv4.conf.default.accept_source_route = 0
kernel.sysrq = 0
kernel.core_uses_pid = 1
net.ipv4.tcp_syncookies = 1
kernel.msgmnb = 65536
kernel.msgmax = 65536
kernel.shmmax = 68719476736
kernel.shmall = 4294967296
net.ipv4.tcp_max_tw_buckets = 6000
net.ipv4.tcp_sack = 1
net.ipv4.tcp_window_scaling = 1
net.ipv4.tcp_rmem = 4096 87380 4194304
net.ipv4.tcp_wmem = 4096 16384 4194304
net.core.wmem_default = 8388608
net.core.rmem_default = 8388608
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
net.core.netdev_max_backlog = 262144
net.core.somaxconn = 262144
net.ipv4.tcp_max_orphans = 3276800
net.ipv4.tcp_max_syn_backlog = 262144
net.ipv4.tcp_timestamps = 0
net.ipv4.tcp_synack_retries = 1
net.ipv4.tcp_syn_retries = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_mem = 94500000 915000000 927000000
net.ipv4.tcp_fin_timeout = 1
net.ipv4.tcp_keepalive_time = 30
net.ipv4.ip_local_port_range = 1024 65000
EOF

sysctl -p
echo "configure sysctl is complete!"


yum install -y gcc gcc-c++ openssl openssl-devel pcre-devel
cd /data/tools
tar -zxvf pcre-8.37.tar.gz -C /usr/local/src/
cd /usr/local/src/pcre-8.37/
./configure --prefix=/usr/local/pcre
make &&make install
cd /data/tools
tar -zxvf libevent-2.0.22-stable.tar.gz -C /usr/local/src/
cd /usr/local/src/libevent-2.0.22-stable/
./configure --prefix=/usr/local/libevent
make && make install
cd /usr/local/libevent/
ln -s /usr/local/libevent/include /usr/include/libevent
echo "/usr/local/libevent/lib" >>/etc/ld.so.conf.d/libevent.conf
ldconfig -pv | grep libevent
groupadd nginx
useradd -r -g nginx -s /sbin/nologin -M nginx
cd /data/tools
tar -zxvf nginx-1.8.0.tar.gz -C /usr/local/src/
cd /usr/local/src/nginx-1.8.0/
./configure \
--conf-path=/etc/nginx/nginx.conf  \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--pid-path=/var/run/nginx.pid  \
--lock-path=/var/lock/nginx.lock  \
--user=nginx  \
--group=nginx  \
--with-http_ssl_module \
--with-http_flv_module \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--http-client-body-temp-path=/var/tmp/nginx/client/ \
--http-proxy-temp-path=/var/tmp/nginx/proxy/ \
--http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ \
--with-pcre=/usr/local/src/pcre-8.37
ln -s /var/log/nginx/error.log /etc/nginx/error.log
ln -s /var/log/nginx/access.log /etc/nginx/access.log
ln -s /var/run/nginx.pid /etc/nginx/nginx.pid
ln -s /var/lock/nginx.lock /etc/nginx/nginx.lock
make && make install
cd /usr/local/nginx
mkdir -pv /var/tmp/nginx/client
mkdir -pv /var/tmp/nginx/proxy
mkdir -pv /var/tmp/nginx/fcgi
echo "PATH=$PATH:/usr/local/nginx/sbin" >>/etc/profile
source /etc/profile
cat >>/etc/init.d/nginx<<EOF
\#!/bin/bash
\# chkconfig: 2345 65 45
\# description: nginx serverdaemon
prog=/usr/local/nginx/sbin/nginx
lockfile=/var/lock/nginx.lock
pidfile=/var/run/nginx.pid
start(){
#以下的$、`、#前都加了转义符号"\"
      [ -f \$lockfile ] && echo"nginx is started." && exit
      echo -n "nginx is starting.."
      sleep 1 && echo -n"."
      \$prog &&  echo -e  "\$space[\033[32m OK\033[0m]" && touch \$lockfile || echo  -e "\$space[\033[31m failed\033[0m]"
}
stop(){
      [ ! -f \$lockfile ] && echo"nginx is stopped." && exit
      echo -n "nginx is stopping.."
      sleep 1 && echo -n"."
      \$prog -s stop && echo  -e "\$space[\033[32m OK \033[0m]"&& rm -f \$lockfile || echo  -e"\$space[\033[31m failed \033[0m]"
}
status(){
      [ ! -f \$pidfile ] && echo"nginx is stoped" || echo "\`cat \$pidfile\`,nginx is running"
}

case "\$1" in
start)
      start
      ;;
stop)
      stop
      ;;
restart)
      stop
      start
      ;;
status)
      status
      ;;
*)
      echo "UASGE IS:start|stop|restart|status"
      ;;
esac

EOF
#生成后,需要将“\#”修改会“#”,而“\`”及“\$”则会自动转为“`”以及“$”
sed -i 's/\\\#/\#/g' /etc/init.d/nginx
chmod  +x /etc/init.d/nginx
chkconfig --add nginx
chkconfig --list nginx
service nginx start
echo "suessfull!!"



通过以上操作,我们就很方便的在shell脚本中实现生成文件或者脚本了。

还有请注意,如“\”转义符不能同时有两个以上,否则也会无法写入。



运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-297266-1-1.html 上篇帖子: Tcpdump命令参数详解 下篇帖子: centos7文字界面下安装Lynx并文字上网记录
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表