bash 将 SQL 查询和命令输出到 shell 脚本中的文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30759941/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Output of SQL query and command to a file in shell script
提问by Nair
I have the following shell script which runs a SQL query and a command which sends the output as an email. The issue is I am able to send only the SQL output. Not the output of the for loop. I tried to give the "EOF" after the for loop but then it gives a syntax error. Please let me know how to send both the output in an email.
我有以下 shell 脚本,它运行 SQL 查询和将输出作为电子邮件发送的命令。问题是我只能发送 SQL 输出。不是 for 循环的输出。我试图在 for 循环之后给出“EOF”,但它给出了一个语法错误。请让我知道如何在电子邮件中发送这两个输出。
Thanks & Regards, Akhil
感谢和问候, 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'`" [email protected]
rm output_temp.lst
回答by tripleee
You are overwriting the file.
您正在覆盖文件。
echo moo >output.lst
echo bar >output.lst
Now, output.lst
only contains bar
.
现在,output.lst
只包含bar
.
The trick is to append, instead of overwrite.
诀窍是追加,而不是覆盖。
echo moo >output.lst
echo bar >> output.lst
Your script has multiple additional problems, so it is not really clear what you actually want it to do. I'm guessing something like this.
您的脚本有多个额外的问题,因此您并不清楚您真正想要它做什么。我猜是这样的。
sql things >output.lst
for var in things; do stuff; done | sed 's/ *$//' >>output.lst
... which coincidentally could also be done with a single redirection if you wanted to, by running the commands in a subshell, and redirecting output from the subshell:
...巧合的是,如果您愿意,也可以通过单个重定向来完成,方法是在子 shell 中运行命令,并重定向来自子 shell 的输出:
( sql things
for var in things; do stuff; done | sed 's/ *$//' ) >output.lst
With these speculations, your entire script could be
有了这些推测,你的整个脚本可能是
#!/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'`" [email protected] <$output
Even the permanent output file could be avoided if you like; just pipe to mail
.
如果您愿意,甚至可以避免永久输出文件;只需管道到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'`" [email protected]
(... assuming your sed
understands \|
to mean alternation in a regex).
(...假设您sed
理解\|
为正则表达式中的交替)。