简体   繁体   English

在shell脚本循环中假脱机多个文件

[英]spool multiple files in loop of shell script

I am trying to spool multiple files to reduce the manual work.我正在尝试后台处理多个文件以减少手动工作。

The SQL file (spool_csv.sql) have a select statement which i need to spool as a CSV file like below SQL 文件 (spool_csv.sql) 有一个 select 语句,我需要将其作为 CSV 文件进行假脱机,如下所示

set colsep ','
set linesize 9999
set trimspool on
set heading on
set pagesize 0 embedded on
set wrap off
set feedback off
set newpage 0
set verify off
set arraysize 5000
SET TERMOUT OFF
spool C:\Users\spool_csv\csv_03\csv_file_03-jan_&1..csv
select * from CRAFT_MVT_H where code = '&1';
spool off;
exit;

and then i have simple shell script (spool_csv.sh) which is looping through a file to pass parameter to SQL file and to change the name of spool file on runtime and also for where clause of SELECT statement.然后我有简单的 shell 脚本 (spool_csv.sh),它循环遍历一个文件以将参数传递给 SQL 文件并在运行时更改假脱机文件的名称以及 SELECT 语句的 where 子句。

for line in `cat chartfield1_03.txt`
do
  echo $line
  sqlplus -s user/password@SID @spool_03.sql $line
done 

chartfield1_03.txt contains -- chartfield1_03.txt 包含——

778
769
741 
728
739
759
737
752
710
734
747

i need to have separate csv file for each line (say value) of chartfield1_03.txt我需要为 chartfield1_03.txt 的每一行(比如值)有单独的 csv 文件

so it will be like所以它会像

csv_file_03-jan_778.csv
csv_file_03-jan_769.csv
csv_file_03-jan_741.csv
and so on....

but when i run the shell script it only creating one CSv file that is for last line of chartfield1_03.txt ie csv_file_03-jan_747.csv但是当我运行 shell 脚本时,它只创建一个用于 chartfield1_03.txt 最后一行的 CSV 文件,即 csv_file_03-jan_747.csv

i am not sure why other file are not creating while i am passing the spool file as parameter and calling it in a loop so each time it should open a new SQL session and create the new spool file.我不确定为什么在我将假脱机文件作为参数传递并在循环中调用它时没有创建其他文件,因此每次它都应该打开一个新的 SQL 会话并创建新的假脱机文件。

Please help me understand what i am missing here !!请帮助我理解我在这里缺少什么!!

So in the end i found out why it was not working as i missed to EOF in SQLPLUS command after adding this as below it is working fine.所以最后我发现了为什么它不工作,因为我在 SQLPLUS 命令中错过了 EOF 在添加如下后它工作正常。

Solution as i find it.我找到的解决方案。

#!/bin/bash

for line in `cat chartfield1_03.txt`
do
  echo $line
  dt1 = `sqlplus -s HEW/hew_dba14@DEVHEW<<EOF
  whenever sqlerror exit sql.sqlcode;
  @spool_03.sql $line;
EOF` &

done

One possible solution is to create on each sqlplus execution the same file and rename it in shell.一种可能的解决方案是在每次sqlplus执行时创建相同的文件并在 shell 中重命名它。 For this you can change line:为此,您可以更改行:

spool C:\Users\spool_csv\csv_03\csv_file_03-jan_&1..csv

to be成为

spool C:\Users\spool_csv\csv_03\csv_file_03-jan_..csv

and in shell script:并在shell脚本中:

sqlplus -s user/password@SID @spool_03.sql $line

to be成为

sqlplus -s user/password@SID @spool_03.sql 
mv "C:\Users\spool_csv\csv_03\csv_file_03-jan_..csv" "C:\Users\spool_csv\csv_03\csv_file_03-jan_${line}.csv"

My solution.我的解决方案。

please provide any other solution if you have in your mind :)如果您有任何其他解决方案,请提供任何其他解决方案:)

#!/bin/bash

for line in `cat chartfield1_03.txt`
do
  echo $line
  dt1 = `sqlplus -s HEW/hew_dba14@DEVHEW<<EOF
  whenever sqlerror exit sql.sqlcode;
  @spool_03.sql $line;
EOF` &

done

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM