bash - Output of SQL query and command to a file in shell script -


i have following shell script runs sql query , command sends output email. issue able send sql output. not output of loop. tried give "eof" after loop gives syntax error. please let me know how send both output in email.

thanks & regards, akhil

#!/bin/bash source $home/.bash_profile  cd /home/cron  wfvar="red blue green"   echo " " > /home/cron/output.lst sqlplus -s user/test@db <<eof set linesize 55 pages 500 spool output_temp.lst; set head off; select sysdate dual; set head on; spool off; eof  name in ${wfvar};   pmcmd getworkflowdetails -sv repository ${name} | grep -e "workflow:" -e "workflow run status:" -e "end time:"  done  sed -e 's/ *$//' output_temp.lst > output.lst cat /home/cron/output.lst | mail -s "output - `date '+%d-%m-%y'`" akhil@gmail.com rm output_temp.lst 

you overwriting file.

echo moo >output.lst echo bar >output.lst 

now, output.lst contains bar.

the trick append, instead of overwrite.

echo moo >output.lst echo bar >> output.lst 

your script has multiple additional problems, not clear want do. i'm guessing this.

sql things >output.lst var in things; stuff; done | sed 's/ *$//' >>output.lst 

... coincidentally done single redirection if wanted to, running commands in subshell, , redirecting output subshell:

( sql things   var in things; stuff; done | sed 's/ *$//' ) >output.lst 

with these speculations, entire script be

#!/bin/bash # source $home/.bash_profile   ######## xxx don't  output=/home/cron/output.lst  sqlplus -s user/test@db <<eof >$output   # capture output sql set linesize 55 pages 500 spool output_temp.lst; set head off; select sysdate dual; set head on; spool off; eof  name in red blue green;   pmcmd getworkflowdetails -sv repository "$name" done | grep -e "workflow:" -e "workflow run status:" -e "end time:" | # xxx: benefit refactoring grep x | sed y sed '/x/!d;y' sed -e 's/ *$//' >> $output # xxx: fixed useless use of cat award mail -s "output - `date '+%d-%m-%y'`" akhil@gmail.com <$output 

even permanent output file avoided if like; pipe mail.

#!/bin/bash  ( sqlplus -s user/test@db <<__eof     set linesize 55 pages 500     spool output_temp.lst;     set head off;     select sysdate dual;     set head on;     spool off; __eof    name in red blue green;     pmcmd getworkflowdetails -sv repository "$name"   done |   sed -e '/workflow:\|workflow run status:\|end time:/!d' -e 's/ *$//' ) |  mail -s "output - `date '+%d-%m-%y'`" akhil@gmail.com 

(... assuming sed understands \| mean alternation in regex).


Comments

Popular posts from this blog

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -

javascript - oscilloscope of speaker input stops rendering after a few seconds -