In a shell script written by OPS, it is sometimes necessary to put some content directly into a file, such as configuring some content in a shell script to regenerate into a shell script, using the cat command and redirect symbol "<<" and the use of EOF. However, when you generate a shell script with a redirect symbol in a shell script, you encounter some problems, such as the presence of a special symbol "#", "'", "$" in the content, and (if you start with "#", the need to add the escape character "\") redirection ignores these special symbols. The resulting shell script will not run, at this point only need to escape the symbol "\" Before these special symbols, the following is an automatic installation Nginx script:
#!/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 xiaoyaoecho "Configure sysctl" mv /etc/ sysctl.conf /etc/sysctl.conf.bakcat >>/etc/sysctl.conf<<eofnet.ipv4.ip_forward = 0net.ipv4.conf.default.rp_filter = 1net.ipv4.conf.default.accept_source_route = 0kernel.sysrq = 0kernel.core_uses_pid = 1net.ipv4.tcp_syncookies = 1kernel.msgmnb = 65536kernel.msgmax = 65536kernel.shmmax = 68719476736kernel.shmall = 4294967296net.ipv4.tcp_max_tw_buckets = 6000net.ipv4.tcp_sack = 1net.ipv4.tcp_window_scaling = 1net.ipv4.tcp_rmem = 4096 87380 4194304net.ipv4.tcp_wmem = 4096 16384 4194304net.core.wmem_default = 8388608net.core.rmem_default = 8388608net.core.rmem_max = 16777216net.core.wmem_max = 16777216net.core.netdev_max_backlog = 262144net.core.somaxconn = 262144net.ipv4.tcp_max_orphans = 3276800net.ipv4.tcp_max_syn_ backlog = 262144net.ipv4.tcp_timestamps = 0net.ipv4.tcp_synack_retries = 1net.ipv4.tcp_syn_retries = 1net.ipv4.tcp_tw_recycle = 1net.ipv4.tcp_tw_reuse = 1net.ipv4.tcp_mem = 94500000 915000000 927000000net.ipv4.tcp_fin_timeout = 1net.ipv4.tcp_keepalive_time = 30net.ipv4.ip_local_port_range = 1024 65000eofsysctl -pecho "configure sysctl is complete!" Yum install -y gcc gcc-c++ openssl openssl-devel pcre-develcd /data/toolstar -zxvf pcre-8.37.tar.gz -c /usr/local/src/cd /usr/local/ src/pcre-8.37/./configure --prefix=/usr/local/pcremake &&make installcd /data/ toolstar -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/libeventmake && make Installcd /usr/local/libevent/ln -s /usr/local/libevent/include /usr/include/libeventecho "/usr/local/libevent/lib" >>/etc/ld.so.conf.d/libevent.confldconfig -pv | grep libeventgroupadd nginxuseradd -r -g nginx -s /sbin/nologin -M nginxcd /data/toolstar -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.37ln -s /var/log/ nginx/error.log /etc/nginx/error.logln -s /var/log/nginx/access.log /etc/nginx/ Access.logln -s /var/run/nginx.pid /etc/nginx/nginx.pidln -s /var/lock/nginx.lock /etc/nginx/nginx.lockmake && make installcd /usr/local/nginxmkdir -pv /var/tmp/nginx/clientmkdir -pv /var/tmp/nginx/proxymkdir -pv /var/tmp/nginx/fcgiecho "path= $PATH:/usr/local/nginx/sbin" >>/eTc/profilesource /etc/profilecat >>/etc/init.d/nginx<<eof\#!/bin/bash\# chkconfig: 2345 65 45\# description: nginx serverdaemonprog=/usr/local/nginx/sbin/ Nginxlockfile=/var/lock/nginx.lockpidfile=/var/run/nginx.pidstart () {#以下的 $, ', #前都加了转义符号 ' \ ' [ -f \ $lockfile ] && echo "nginx is started." && exit echo -n "Nginx is starting ..." sleep 1 && echo -n "." \ $prog && echo -e "\ $space [\033[ 32M&NBSP;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&NBSP;OK&NBSP;\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" instart) start ;; Stop) stop ;; Restart) stop start ;; Status) status ;; *) echo "Uasge is:start|stop|restart|status" ;; After the esaceof# is generated, the "\#" will need to be modified "#" and "\" and "\$" will be automatically converted to "'" and "$" sed -i ' s/\\\#/\#/g ' /etc/init.d/nginxchmod +x /etc/init.d/nginxchkconfig --add nginxchkconfig --list nginxservice nginx startecho "suessfull!!"
By doing this, we can easily implement the generated file or script in the shell script.
Also, please note that the "\" escape character cannot be more than two at the same time, otherwise it will not be writable.
Considerations for using the cat and redirect symbol <<eof in a CentOS system shell script