shellスクリプト、複数行のコンテンツをファイルに書き込む

2442 ワード

例:
#! /bin/bash

echo " "
cat>test<<EOF
 shell 
this is a file created by shell.
we want to make a good world.
EOF

このうち、test<の間にスペースはありません.また、ファイル名とEOFの内容を生成し、変数をサポートします.注意:この方法はtestファイルを上書きし、testファイルを追加して書き込む場合はcat>>test<方式を使用します.
プロジェクトの例:
プロジェクトtomcatにスクリプトを自動的にインストールし、起動スクリプトを生成し、crontab起動実行配置環境に書き込む必要があります:centos 7
#!/bin/bash

echo "----web apache----"
web_source="web_apache.tar.gz"
web_target="/data"

echo "----unzip web_apache.tar.gz----"
tar zxvf $web_source -C $web_target
#  “apache”

# tomcat 
sh $web_target/apache/bin/startup.sh

# web_up.sh , 
cat>$web_target/web_up.sh<<EOF
#!/bin/bash

# nginx
ps -fe|grep nginx|grep -v grep
if [ \$? -ne 0 ]
then
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
fi

# tomcat
ps -fe|grep tomcat|grep -v grep
if [ \$? -ne 0 ]
then
sh $web_target/apache/bin/startup.sh
fi
EOF

# 
chmod 755 $web_target/web_up.sh

# 
echo "@reboot (sleep 20; sh $web_target/web_up.sh)" >> /var/spool/cron/root