我是靠谱客的博主 谦让期待,这篇文章主要介绍在centos系统shell脚本中cat和重定向符号<,现在分享给大家,希望可以做个参考。

在运维人员编写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
#!/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[33[32m OK33[0m]" && touch $lockfile || echo  -e "$space[33[31m failed33[0m]" } stop(){       [ ! -f $lockfile ] && echo"nginx is stopped." && exit       echo -n "nginx is stopping.."       sleep 1 && echo -n"."       $prog -s stop && echo  -e "$space[33[32m OK 33[0m]"&& rm -f $lockfile || echo  -e"$space[33[31m failed 33[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脚本中实现生成文件或者脚本了。


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


转载于:https://blog.51cto.com/canonind/1870110

最后

以上就是谦让期待最近收集整理的关于在centos系统shell脚本中cat和重定向符号<的全部内容,更多相关在centos系统shell脚本中cat和重定向符号<内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(65)

评论列表共有 0 条评论

立即
投稿
返回
顶部