Intersystems Cache SQL shell:如何将查询的输出假脱机到文件中?

发布于 2024-10-26 11:58:49 字数 187 浏览 1 评论 0原文

我正在尝试在 UNIX 中创建一个脚本来查询缓存数据库。我可以获得输出,但如何将输出写入文件> 我使用了以下过程:

cache << EOF
DO \$SYSTEM.SQL.Shell()
SELECTMODE DISPLAY
SELECT * from .....
GO
EXIT
EOF

I am trying to create a script in UNIX to query the Cache database. I could get the output but how do I write the output to a file>
I used the following procedure:

cache << EOF
DO \$SYSTEM.SQL.Shell()
SELECTMODE DISPLAY
SELECT * from .....
GO
EXIT
EOF

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

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

发布评论

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

评论(2

如此安好 2024-11-02 11:58:49

如果您将其用于报告/网页/等,我会查看 Intersystems 为 Perl、Python、Ruby 等提供的内置模块。这些应该为您提供更清晰的数据库界面。

也就是说,我出于监控目的进行了一些外部调用,但由于我试图从数据库(通常是内部)获取内容,因此不使用语言 API。在这些情况下我使用 Perl。我使用 Perl 是因为这样做我需要解析菜单和其他我不想要的东西(通过 API 是无法得到的)。以下是查看缓存中存在的所有用户的简单示例。

#!/usr/bin/perl

use strict;

my $username = 'user';
my $password = 'password'; 

my $val = `csession $instance -U %SYS << done
$username
$password

d ^SECURITY
1
3
*
*



h
done`;

my @users = split(/\n/, $val);
my $in_users = 0;
my $output = '';
foreach (@users){
     if($in_users){
    chomp($_);
    if($_ eq ''){
        #no longer listing users
        $in_users = 0;
    } else {
            $output .= "$_\n";
        } 
  }
  $in_users = 1 if($_ =~ m/^\-\-\-\-/);  # I started a the user list 
}

print $output; # this prints out to the console

#This prints to a file.
open(OUTFILE, ">outfile.txt");
print OUTFILE $output;

查看以下有关 Perl 和 Python API 的链接
http://docs.intersystems.com/cache20102 /csp/docbook/DocBook.UI.Page.cls?KEY=GBPL

http://docs.intersystems.com/cache20102 /csp/docbook/DocBook.UI.Page.cls?KEY=GBPY

If you are using this for reports/webpages/etc I would look at the built in modules that Intersystems provides for Perl, Python, Ruby, etc. These should give you a cleaner interface into the database.

That said, I do some external calls for monitoring purposes that don't use the language API because of what I am trying to get from the database (usually internals). In those cases I use Perl. I use Perl to because doing it this way I need to parse out the menus and other things I don't want (which you won't get with the APIs). The following is a simple example looking at all the users that exist in Cache.

#!/usr/bin/perl

use strict;

my $username = 'user';
my $password = 'password'; 

my $val = `csession $instance -U %SYS << done
$username
$password

d ^SECURITY
1
3
*
*



h
done`;

my @users = split(/\n/, $val);
my $in_users = 0;
my $output = '';
foreach (@users){
     if($in_users){
    chomp($_);
    if($_ eq ''){
        #no longer listing users
        $in_users = 0;
    } else {
            $output .= "$_\n";
        } 
  }
  $in_users = 1 if($_ =~ m/^\-\-\-\-/);  # I started a the user list 
}

print $output; # this prints out to the console

#This prints to a file.
open(OUTFILE, ">outfile.txt");
print OUTFILE $output;

Check out the following links about the Perl and Python API
http://docs.intersystems.com/cache20102/csp/docbook/DocBook.UI.Page.cls?KEY=GBPL
and
http://docs.intersystems.com/cache20102/csp/docbook/DocBook.UI.Page.cls?KEY=GBPY

深居我梦 2024-11-02 11:58:49

这有点 hackish,但以下应该可以工作:

echo -e "username\npassword\nDO \$SYSTEM.SQL.Shell()\nfoo\nbar\nH\n" | cache > output

您在 Caché 中输入的每个命令后面都跟有一个“\n”以指示行返回。然后将其通过管道传输到新会话,并将其输出写入文件。

It's a bit hackish, but the following should work:

echo -e "username\npassword\nDO \$SYSTEM.SQL.Shell()\nfoo\nbar\nH\n" | cache > output

Each command you would enter in Caché is followed by a "\n" to indicate a line return. That is then piped into a new session and its output is written to the file.

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