在centos系统shell脚本中cat和重定向符号<<EOF结合使用的注意事项
2016-11-07 09:46
633 查看
在运维人员编写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脚本中实现生成文件或者脚本了。
还有请注意,如“\”转义符不能同时有两个以上,否则也会无法写入。
相关文章推荐
- centos shell编程5 LANMP一键安装脚本 lamp sed lnmp 变量和字符串比较不能用-eq cat > /usr/local/apache2/htdocs/index.php <<EOF重定向 shell的变量和函数命名不能有横杠 平台可以用arch命令,获取是i686还是x86_64 curl 下载 第三十九节课
- Shell中重定向<<EOF注意事项
- centos shell基础 alias 变量单引号 双引号 history 错误重定向 2>&1 jobs 环境变量 .bash_history source配置文件 nohup & 后台运行 cut,sort,wc ,uniq ,tee ,tr ,split, paste cat> 2.txt <<EOF 通配符 glob模式 发邮件命令mail 2015-4-8 第十二节课
- shell脚本使用cat和EOF组合添加和删除文件内容
- shell中使用判断符号[ ]的注意事项
- 在shell脚本中使用ls命令的注意事项
- 在存储过程中使用系统存储过程sp_Excute的注意事项
- return 于try{}catch(Exception e){}finally{}语句结合使用的一点注意事项。
- 使用shell脚本进行服务器系统监控——进程监控
- PHP调用shell脚本注意事项
- hibernate 与spring结合,hibernatedaoSupport使用注意事项
- 64位系统使用VS2010开发32位ASP.NET应用--注意事项
- 使用shell脚本进行服务器系统监控——文件系统监控(4)
- 使用shell脚本进行服务器系统监控——文件系统监控(5)
- C#中的@符号的使用及注意事项
- shell脚本中字符和文件的判断及字符串入参的注意事项
- C# 中的 @ 符号的使用及注意事项
- C# 中的 @ 符号的使用及注意事项
- 使用shell脚本进行服务器系统监控——系统负载监控(2)
- 使用shell脚本进行服务器系统监控——系统负载监控(3)