从 Perl 脚本中维护外部 shell 环境

发布于 2024-12-08 00:23:14 字数 803 浏览 0 评论 0原文

我有一个 Perl 脚本,需要调用 IBM db2 来处理一些命令。问题是 db2 连接在每次调用时都会被破坏

这不起作用:

$> db2 connect to foo
$> perl -e 'print `db2 list tables for schema bar|some_filter`;'

这也不起作用:

$> perl -e 'print `db2 connect to foo`; print `db2 list tables for schema bar|some_filter`;'

这也不起作用:

$> perl -e 'print `db2 connect to foo && db2 list tables for schema bar|some_filter`;'

在每种情况下,在执行第二个命令时,连接都会丢失。

这可行:

#!/usr/bin/perl
print `db2 connect to foo`;
print `db2 list tables for schema bar`;

但这不行:

#!/usr/bin/perl
print `db2 connect to foo`;
print `db2 "select count * from bar"`;

解决方法是从 Perl 生成 bash 脚本并执行它,但直接执行会更好。有办法吗?

I have a Perl script which needs to invoke IBM db2 to process some commands. The trouble is that the db2 connection is clobbered at each invocation

This doesn't work:

gt; db2 connect to foo
gt; perl -e 'print `db2 list tables for schema bar|some_filter`;'

nor does this:

gt; perl -e 'print `db2 connect to foo`; print `db2 list tables for schema bar|some_filter`;'

nor does this:

gt; perl -e 'print `db2 connect to foo && db2 list tables for schema bar|some_filter`;'

In each case the connection is lost by the time the second command is executed.

This works:

#!/usr/bin/perl
print `db2 connect to foo`;
print `db2 list tables for schema bar`;

but this doesn't:

#!/usr/bin/perl
print `db2 connect to foo`;
print `db2 "select count * from bar"`;

A workaround would be to generate a bash script from Perl and execute that, but it would be nicer to do it directly. Is there a way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

雨后咖啡店 2024-12-15 00:23:14

db2 的每个单独调用都必须重新连接,然后才能对数据库执行任何操作,并且当调用结束时,连接将终止。所以你观察到的行为是可以预料的。

如果您使用 Perl,请认真考虑使用 DBI

如果这不是一个选项,那么您需要考虑从 Perl 脚本运行 db2 程序作为子进程,并有一个通向该子进程的管道(您可以在该子进程上编写命令)和一个从该子进程引回的管道。您需要将命令写入 db2,然后能够解析返回的数据,以便您可以知道它何时完成响应并处理错误消息和数据。必须有相应的模块(可能是核心模块 IPC::Open3IPC::Open2)。

Each separate invocation of db2 has to connect anew before it can do anything with the database, and when the invocation ends, the connection is terminated. So your observed behaviour is to be expected.

If you're using Perl, seriously consider using DBI.

If that's not an option, then you need to consider running the db2 program from your Perl script as a sub-process with a pipe leading to it on which you write commands and a pipe leading back from it. You would need to write your commands to db2 and then be able to parse the returned data so you can tell when it has finished responding and deal with error messages and data. There must be modules for that (maybe core module IPC::Open3 or IPC::Open2).

扬花落满肩 2024-12-15 00:23:14

为什么不使用 DBI 而不是 shell 脚本?然后,您可以根据需要维持连接并重复使用现有连接。

Why not use DBI instead of a shell script? Then you can maintain the connection and reuse the existing connection for as long as you want.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文