[Novalug] Thanks for help with shell script

Jon LaBadie novalugml@jgcomp.com
Mon Mar 4 12:57:17 EST 2013


On Mon, Mar 04, 2013 at 11:03:23AM -0500, Tony Jones wrote:
> Wanted to says thanks to those who helped me with appending commands to a
> shell script.  I used the "here" document to write to a script, instead of
> using the echo statement like this:
> 
> #/bin/sh
> # Here document containing the body of the generated script
> RPM_CRON_FILE=/tmp/rpmtest.cron
> (
> cat <<''EOF'
> #!/bin/sh
> # Query for mysql rpm packages
> YMD=$(date +%Y%m%d")
> RPM_LOG_FILE=/tmp/$YMD-test.log
> /bin/rpm -qa mysql > $RPM_LOG_FILE
> EOF
> )  > $RPM_CRON_FILE
> chmod 0700 $RPM_CRON_FILE
> 
> So, I learned about "here" documents and not to use back-ticks.

Glad you picked up so much including quoting the eof word.

Couple of comments.
1. Extra single quote on "cat" CL
2. Missing double quote on date CL
3. To "pretty" things up you can indent the here document
   with tabs if you use <<- instead of << .
4. No need for the subshell for redirection.  The cat
   command can be redirected, either before or after
   the <<EOF.

#/bin/sh
# Here document containing the body of the generated script

RPM_CRON_FILE=/tmp/rpmtest.cron

cat > $RPM_CRON_FILE <<-'EOF' 
	#!/bin/sh
	# Query for mysql rpm packages
	YMD=$(date +"%Y%m%d")
	RPM_LOG_FILE=/tmp/$YMD-test.log
	/bin/rpm -qa mysql > $RPM_LOG_FILE
EOF

chmod 0700 $RPM_CRON_FILE

-- 
Jon H. LaBadie                  novalugml@jgcomp.com
 11226 South Shore Rd		(703) 787-0688 (H)
 Reston, VA  20190		(609) 477-8330 (C)



More information about the Novalug mailing list