SQL 查询和命令的输出到 shell 脚本中的文件
Output of SQL query and command to a file in shell script
我有以下 shell 脚本,它运行 SQL 查询和一个将输出作为电子邮件发送的命令。问题是我只能发送 SQL 输出。不是 for 循环的输出。我试图在 for 循环之后给出 "EOF",但它给出了语法错误。请让我知道如何通过电子邮件发送这两个输出。
感谢和问候,
阿基尔
#!/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
您正在覆盖文件。
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
理解 \|
表示正则表达式中的交替)。
我有以下 shell 脚本,它运行 SQL 查询和一个将输出作为电子邮件发送的命令。问题是我只能发送 SQL 输出。不是 for 循环的输出。我试图在 for 循环之后给出 "EOF",但它给出了语法错误。请让我知道如何通过电子邮件发送这两个输出。
感谢和问候, 阿基尔
#!/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
您正在覆盖文件。
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
理解 \|
表示正则表达式中的交替)。