我有以下运行SQL查询的shell脚本和一个将输出作为电子邮件发送的命令。问题是我只能发送SQL输出。不是for循环的输出。我试图给出" EOF"在for循环之后但是它给出了语法错误。请让我知道如何通过电子邮件发送输出。
谢谢&问候, 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 from dual;
set head on;
spool off;
EOF
for name in ${wfVAR}; do
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
答案 0 :(得分:1)
您正在覆盖该文件。
echo moo >output.lst
echo bar >output.lst
现在,output.lst
仅包含bar
。
诀窍是追加,而不是覆盖。
echo moo >output.lst
echo bar >> output.lst
您的脚本还有其他一些问题,因此您实际上并不清楚它的实际用途。我猜是这样的。
sql things >output.lst
for var in things; do stuff; done | sed 's/ *$//' >>output.lst
...如果您愿意,也可以通过在子shell中运行命令并从子shell重定向输出来完成单个重定向:
( sql things
for var in things; do stuff; done | sed 's/ *$//' ) >output.lst
通过这些推测,您的整个脚本可能是
#!/bin/bash
# source $HOME/.bash_profile ######## XXX probably don't do this
output=/home/cron/output.lst
sqlplus -s user/test@DB <<EOF >$output # Capture output from SQL
set linesize 55 pages 500
spool output_temp.lst;
set head off;
select sysdate from dual;
set head on;
spool off;
EOF
for name in red blue green; do
pmcmd getworkflowdetails -sv REPOSITORY "$name"
done |
grep -e "Workflow:" -e "Workflow run status:" -e "End time:" |
# XXX: could benefit from refactoring grep x | sed y to just 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
如果你愿意,甚至可以避免永久输出文件;只需管道mail
。
#!/bin/bash
( sqlplus -s user/test@DB <<__EOF
set linesize 55 pages 500
spool output_temp.lst;
set head off;
select sysdate from dual;
set head on;
spool off;
__EOF
for name in red blue green; do
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
(...假设您的sed
理解\|
表示正则表达式中的替换。)