在运维人员编写shell脚本中,有时会需要将一些内容直接放在到一个文件,比如在一个shell脚本中配置一些内容再生成一个shell脚本,此时可以使用到cat命令和重定向符号“<<”以及EOF的使用。但是,在shell脚本中使用重定向符号生成shell脚本时,会遇到一些问题,比如,内容中含有特殊符号"#","`","$"时,(如果以“#”开头,则需要加转义符“\”)重定向会忽略这些特殊符号,而导致生成的shell脚本无法运行,此时只需要在这些特殊符号前加转义符号“\”即可,如下是一个自动安装nginx的脚本:
#!/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脚本中实现生成文件或者脚本了。
还有请注意,如“\”转义符不能同时有两个以上,否则也会无法写入。
转载于:https://blog.51cto.com/canonind/1870110