Filipe Brandenburger
2009-Feb-07 03:15 UTC
[CentOS] Redirection in shell (was: squid HA failover?)
Hi, Regarding the last example creating the config files with several echos with redirection: On Fri, Feb 6, 2009 at 20:37, J Potter <jpotter-centos at codepuppy.com> wrote:> echo "node a.example.com" > /etc/ha.d/ha.cf > echo "node b.example.com" >> /etc/ha.d/ha.cf > echo "udpport 9000" >> /etc/ha.d/ha.cf > echo "bcast bond0" >> /etc/ha.d/ha.cf > echo "auto_failback off" >> /etc/ha.d/ha.cf > echo "logfile /var/log/ha-log" >> /etc/ha.d/ha.cf > echo "logfacility local0" >> /etc/ha.d/ha.cfI've seen this done too many times, and although this is the most "popular" way of doing it in shell script, it is certainly not the cleanest or the best. For one thing, it is inefficient in that it is opening the file for every command line. And the code is not easy to maintain, as if you change the filename you will have to change line by line. A better way to accomplish the same is: { echo "node a.example.com" echo "node b.example.com" echo "udpport 9000" echo "bcast bond0" echo "auto_failback off" echo "logfile /var/log/ha-log" echo "logfacility local0" } >/etc/ha.d/ha.cf And another way, using "here" documents is: cat <<EDQ >/etc/ha.d/ha.cf node a.example.com node b.example.com udpport 9000 bcast bond0 auto_failback off logfile /var/log/ha-log logfacility local0 EDQ This second one is not so clean in that it "embeds" the document inside the script, but it might be appropriate in some cases. HTH, Filipe