- 求职 : 数据库管理员
- 论坛徽章:
- 0
|
如何在shell中运行sql语句?
還有在
http://www.zdnet.com.cn/developer/code/story/0,3800066897,39225241,00.htm 上學到的方法
Example:
alias sp=sp.sh
sp "connect test/test"
sp "select table_name from user_tables"
sp "select sysdate from dual"
sp "exit"
spd.pl
- #!/usr/bin/perl -w
- # spd.pl - the SQL*Plus daemon server
- use strict;
- use Expect;
- use Socket;
- # set up expect
- # -- timeout after about 10 minutes
- my $timeout = 600;
- # -- scan for the SQL*Plus prompt
- my $prompt = 'SQL>;';
- my $exp = new Expect();
- $exp->;raw_pty(1);
- $exp->;log_stdout(0);
- $exp->;spawn('sqlplus','/nolog') || die "unable to spawn sqlplus: $!";
- $exp->;expect($timeout,'-ex',$prompt) || die $exp->;error();
- print $exp "set sqlprompt $prompt;\n";
- $exp->;expect($timeout,'-ex',$prompt) || die $exp->;error();
- $exp->;clear_accum();
- my $name = "/tmp/sp_$ENV{USER}";
- unlink($name);
- socket(S,PF_UNIX,SOCK_STREAM,0) || die "socket: $!";
- bind(S,sockaddr_un($name)) || die "bind: $!";
- listen(S,SOMAXCONN) || die "listen: $!";
- while(accept(C,S))
- {
- # single threaded to avoid confusion
- my $cmd = <C>;;
- $cmd =~ s/[\r\n]*$//g;
- print $exp $cmd,"\n";
- if ($cmd =~ /^exit;$/mi)
- {
- print C "exit.\n";
- close C;
- last;
- }
- print C $cmd."\n" ;
- $exp->;expect($timeout,$prompt) || die $exp->;error() ;
- print C $exp->;before();
- close C;
- }
- $exp->;soft_close();
- close S;
- unlink($name);
- exit;
复制代码
spc.pl
- #!/usr/bin/perl -w
- # spc.pl
- use Socket;
- use strict;
- my $cmd = join(' ',@ARGV);
- my $name = "/tmp/sp_$ENV{USER}";
- my $proto = getprotobyname('udp');
- $cmd=$cmd.";" if ( not $cmd =~ /.+;$/) ;
- socket(S,PF_UNIX,SOCK_STREAM,0) or die "socket: $!";
- select S; $| = 1; select STDOUT;
- connect(S,sockaddr_un($name)) or die "connect: $!";
- print S $cmd,"\n";
- print while (<S>;);
- close(S);
- exit;
复制代码
sp.sh
- #!/bin/sh
- MF=$0;
- if [ -h $0 ] ; then
- MFILE=$(ls -l $0 );
- MF=${MFILE#*->;};
- fi
- MWD=$( dirname $MF );
- f=/tmp/sp_$USER
- if [ ! -e $f ]; then
- perl $MWD/spd.pl &
- sleep 1 ;
- fi
- perl $MWD/spc.pl "$*"
复制代码 |
|