【问题标题】:Pass results from a MySQL procedure to the Linux command line将结果从 MySQL 过程传递到 Linux 命令行
【发布时间】:2012-06-09 08:20:40
【问题描述】:

我有一个运行以下选择的程序:

select distinct concat( 'php /home/rudyerd-systems/frontend/ClientStatementGenerator.php ', left(user(), locate('@',user())-1), space(1), ca.client_id, space(1), date_format( @pTradingPeriodMonth, '%y%m' ), space(1), date_format( @pTradingPeriodMonth, '%y%m' ),
                    ' > /home/rudyerd-systems.sco/frontend/bash/new_statement_test.html' )
from   ca_client_account ca,
       ca_tranche tr
where  @pTradingPeriodMonth    between tr.dt_value and ifnull( tr.dt_withdrawal, @pTradingPeriodMonth ) and
       ca.client_account_id   = tr.client_account_id
order  by ca.client_id;

并产生以下结果:

| php /home/rudyerd-systems/frontend/ClientStatementGenerator.php scorbet ATP 1205 1205 > /home/rudyerd-systems.sco/frontend/bash/new_statement_test.html                                                                                                          |
| php /home/rudyerd-systems/frontend/ClientStatementGenerator.php scorbet BLB 1205 1205 > /home/rudyerd-systems.sco/frontend/bash/new_statement_test.html                                                                                                          |
| php /home/rudyerd-systems/frontend/ClientStatementGenerator.php scorbet CAR 1205 1205 > /home/rudyerd-systems.sco/frontend/bash/new_statement_test.html                                                                                                          |
| php /home/rudyerd-systems/frontend/ClientStatementGenerator.php scorbet CDR 1205 1205 > /home/rudyerd-systems.sco/frontend/bash/new_statement_test.html                                                                                                          |
| php /home/rudyerd-systems/frontend/ClientStatementGenerator.php scorbet CSP 1205 1205 > /home/rudyerd-systems.sco/frontend/bash/new_statement_test.html                                                                                                          |
| php /home/rudyerd-systems/frontend/ClientStatementGenerator.php scorbet DGC 1205 1205 > /home/rudyerd-systems.sco/frontend/bash/new_statement_test.html                                                                                                          |
| php /home/rudyerd-systems/frontend/ClientStatementGenerator.php scorbet FDR 1205 1205 > /home/rudyerd-systems.sco/frontend/bash/new_statement_test.html                                                                                                          |
| php /home/rudyerd-systems/frontend/ClientStatementGenerator.php scorbet FVN 1205 1205 > /home/rudyerd-systems.sco/frontend/bash/new_statement_test.html                                                                                                          |
| php /home/rudyerd-systems/frontend/ClientStatementGenerator.php scorbet JLM 1205 1205 > /home/rudyerd-systems.sco/frontend/bash/new_statement_test.html                                                                                                          |
| php /home/rudyerd-systems/frontend/ClientStatementGenerator.php scorbet JRA 1205 1205 > /home/rudyerd-systems.sco/frontend/bash/new_statement_test.html                                                                                                          |
| php /home/rudyerd-systems/frontend/ClientStatementGenerator.php scorbet JRP 1205 1205 > /home/rudyerd-systems.sco/frontend/bash/new_statement_test.html                                                                                                          |
| php /home/rudyerd-systems/frontend/ClientStatementGenerator.php scorbet MJO 1205 1205 > /home/rudyerd-systems.sco/frontend/bash/new_statement_test.html                                                                                                          |
| php /home/rudyerd-systems/frontend/ClientStatementGenerator.php scorbet MPW 1205 1205 > /home/rudyerd-systems.sco/frontend/bash/new_statement_test.html                                                                                                          |
| php /home/rudyerd-systems/frontend/ClientStatementGenerator.php scorbet NTC 1205 1205 > /home/rudyerd-systems.sco/frontend/bash/new_statement_test.html                                                                                                          |
| php /home/rudyerd-systems/frontend/ClientStatementGenerator.php scorbet RJA 1205 1205 > /home/rudyerd-systems.sco/frontend/bash/new_statement_test.html                                                                                                          |
| php /home/rudyerd-systems/frontend/ClientStatementGenerator.php scorbet RMM 1205 1205 > /home/rudyerd-systems.sco/frontend/bash/new_statement_test.html                                                                                                          |
| php /home/rudyerd-systems/frontend/ClientStatementGenerator.php scorbet SJC 1205 1205 > /home/rudyerd-systems.sco/frontend/bash/new_statement_test.html                                                                                                          |
| php /home/rudyerd-systems/frontend/ClientStatementGenerator.php scorbet UDB 1205 1205 > /home/rudyerd-systems.sco/frontend/bash/new_statement_test.html 

我想将过程的结果直接输出到 Linux 命令行,以便创建一个包含每个变量的 HTML 页面。此过程将从我要在其中创建 HTML 文件的文件夹中的 bash 脚本运行。

为了完整性,这是完整的过程。

-- ---------------------------------------------------------------------------------
-- pUIGetCliStmtGenList
--
-- This procedure returns the Clients who statements need to be generated for
--
-- ---------------------------------------------------------------------------------

drop procedure if exists pUiGetCliStmtGenList;
delimiter //

create procedure pUiGetCliStmtGenList(
  IN  pTradingPeriodMonth      DATE,
  IN  pUsername                VARCHAR,
  IN  pPassword                VARCHAR,
  OUT pResult                  INT
)
MODIFIES SQL DATA
COMMENT 'Gathers the list of people a statement needs to be created for'
begin
 --
  declare vClient_id           VARCHAR(3);
  declare vAmtTradedSystemCcy  DECIMAL(13,2);
  declare vCount               INT;
  --
  declare EXIT handler for SQLWARNING, SQLEXCEPTION call pRdHandleError( 10000, 'pUiGetCliStmtGenList', vHint );
  --  ***********************************************************************************************************
  set vHint = pTradingPeriodMonth;
  set pResult = 0;

  SELECT DISTINCT CONCAT( 'php /home/rudyerd-systems/frontend/ClientStatementGenerator.php ', left(user(), locate('@',user())-1), space(1), ca.client_id, space(1), date_format( @pTradingPeriod, '%y%m' ), space(1), date_format( @pTradingPeriod, '%y%m' ),
                    ' > /home/rudyerd-systems.sco/frontend/bash/'ca.client_id''@pTradingPeriod'statement.html' )
  FROM   ca_client_account ca,
         ca_tranche tr
  INTO     
  WHERE  @pTradingPeriodMonth    between tr.dt_value and ifnull( tr.dt_withdrawal, @pTradingPeriodMonth ) and
         ca.client_account_id   = tr.client_account_id
  ORDER BY ca.client_id;
  --
  -- If there are no clients
    if vCount <> 0 then
     call pRdHandleError( 1002, 'pUiGetCliStmtGenFlag', vHint );
    end if; 
  --
  --   
  ELSE
  -- Output to Linux Command Line and Generate the HTML pages

  -- Then Convert the pages to pdf
  -- 
  set pResult = 1;
  --
end;
//

delimiter ;

【问题讨论】:

  • 目前尚不清楚文件名应该是什么或它们的内容是什么,或者显示的输出是否旨在成为具有明显指定的重定向的可运行脚本。如果它应该是可运行的,那么同一个文件将被反复覆盖(除非您将&gt; 更改为&gt;&gt;)。我看不到管道字符的来源。你需要更清楚地指定你想要什么。
  • 抱歉,Dennis,new_statement_test.html 将以 3 个字母的客户端 ID 作为前缀,以生成唯一的文件名。即 SJC_new_statement_test.html。我要做的就是通过linux命令行运行上面的每一行,为每个客户端生成一个html页面
  • 我在此基础上写了一个答案。

标签: mysql linux bash command-line


【解决方案1】:

如果您有权访问服务器,以便创建文件,您可以将输出导出为 CSV 文件,例如:

SELECT field1,field2 FROM table1
INTO OUTFILE '/home/output.csv'
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n'

您可以编写解析器或使用其他脚本语言/工具修改条目以满足您的需求...

【讨论】:

    【解决方案2】:

    假设输出显示旨在成为一个可运行的脚本,并且您将 &gt; 更改为 &gt;&gt; 或将其中一个字段值添加到输出文件名以便它们是唯一的,这 可能 做你所追求的:

    mysql -B -u username -p dbname < script.sql | sed 's/|//' | sh
    

    sed 命令会删除管道字符(如果存在)。通过sh 将整个事情通过管道执行运行php 的每一行,并使用给定的参数并将输出重定向到给定的文件。您可能需要更改 mysql 参数以满足您的需要。

    【讨论】:

    • 嗨丹尼斯,谢谢你。我在上面添加了完整的程序。我将从 bash 脚本调用它,并需要将结果输出到命令行。我希望这更有意义,因为我没有提供完整的代码。非常感谢您的帮助
    • @SamCorbet:你没有展示你的 Bash 脚本是如何调用它的。我展示的mysql 命令代表了我的假设。无论如何,您需要做的就是将输出通过管道传输到sh(可能还有sed 命令以摆脱管道字符)。我的答案在哪些方面不起作用?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-03
    • 1970-01-01
    • 2011-10-20
    • 2013-12-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多