2016-05-20 3 views
0

Ich versuche, eine gespeicherte Prozedur von einem Bash-Skript aus einem Python-Skript aufzurufen. Das Script Calling funktioniert gut, da gibt es kein Problem. Das Problem tritt mit dem Parameter für das Feld PRIMARY_KEYS auf. Mit einer einzigen PK es funktioniert gut, aber für mehr PK dh A, B, C, erhalte ich die Oracle-Fehler: ORA-01756: quoted string not properly terminatedGespeicherte Prozedur aus Bash-Skript aufrufen, ORA-01756: Zeichenfolge in Anführungszeichen nicht ordnungsgemäß terminiert

Python:

... 
cmd = 'sh pkg_mypkg.sh \'%s\' \'%s\' my_sp_which_adds_a_primary_key \'%s\'' % db_login_str db_table primary_keys 
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=TRUE) 
... 

Bash:

DB_LOGIN_STR=$1 
DB_TABLE=$2 
FUNC_TO_EXEC=$3 
PRIMARY_KEYS=$4 

... 

function my_sp_which_adds_a_primary_key() { 
    SP_RES=`sqlplus -silent $1 <<-EOM 
    ... 
    BEGIN 
    pkg_mypkg.my_sp_which_adds_a_primary_key($2, $3); 
    END; 
    ... 
    EOM` 
    echo $SP_RES 
} 

... 

function main() { 
    case $FUNC_TO_EXEC in 
    "my_sp_which_adds_a_primary_key") 
     my_sp_which_adds_a_primary_key(DB_LOGIN_STR, DB_TABLE, PRIMARY_KEYS);; 
    esac 
} 

main 

SP:

procedure my_sp_which_adds_a_primary_key(db_table in varchar2, primary_keys in varchar2) 
is 
begin 
execute immediate 'alter table ' || db_table || ' add constraint ' || db_table || '_PK primary_key (' || primary_keys || ') parallel 8'; 
end my_sp_which_adds_a_primary_key; 

Als Disclaimer habe ich tes Diese Funktion wurde direkt in der Benutzeroberfläche der PLSQL-Benutzeroberfläche in einem Testfenster implementiert und funktioniert problemlos mit mehreren PKs. Es gibt ein Problem damit, wie ich zu dem gespeicherten Proc übergehe, wenn ich innerhalb von Bash ausgeführt werde, was ich nicht herausfinden kann. Ich habe versucht, Kombinationen von \'\'%s\'\', \'%s\', \'\"%s\", "%s", ... Nicht sicher, was das richtige Format ist.

Vielen Dank im Voraus,

Antwort

0

Das erste, was ich, ist versuchen würde den Befehl von Python Code wie folgt zu nennen:

cmd = ["pkg_mypkg.sh", db_login_str, db_table, "my_sp_which_adds_a_primary_key", primary_keys] 
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) 

Von the documentation (Hervorhebung von mir):

args should be a sequence of program arguments or else a single string. By default, the program to execute is the first item in args if args is a sequence. If args is a string, the interpretation is platform-dependent and described below. See the shell and executable arguments for additional differences from the default behavior. Unless otherwise stated, it is recommended to pass args as a sequence.

Auch habe ich shell=TRUE sowie den Anruf zu sh entfernt, da sie beide Komplikationen verursachen können, und im Fall der ehemaligen, security risks. Sie sollten stattdessen sicherstellen, dass pkg_mypkg.sh mit einem richtigen Shebang ausführbar ist. Die Dokumentation wieder:

On Unix with shell=True , the shell defaults to /bin/sh . If args is a string, the string specifies the command to execute through the shell. This means that the string must be formatted exactly as it would be when typed at the shell prompt. This includes, for example, quoting or backslash escaping filenames with spaces in them. If args is a sequence, the first item specifies the command string, and any additional items will be treated as additional arguments to the shell itself.


Ohne den vollständigen Shell-Skript zu sehen, ist es schwer zu sagen ist, welche Probleme es auch sein mag, wenn Sie Variablen ohne vorherige $ anrufen, und ich würde empfehlen, den SP-Text Indienststellung eine Datei, anstatt sich auf ein Konstrukt wie heredoc in Backticks zu verlassen. http://shellcheck.net/ kann hilfreich sein, um Fehler und nicht standardmäßige Verwendung zu erkennen.

+0

Vielen Dank für die Vorschläge, ich werde sie in Kürze testen. Im Moment habe ich die Whitespaces innerhalb meiner 'PRIMARY_KEY' var entfernt, so dass' A, B, C' zu 'A, B, C' wurden und das scheint funktioniert zu haben. Irgendetwas Wackeliges passierte, wo "A, B, C" nur "A" wurde. – Qbert

Verwandte Themen