Skip to Main Content

 
Titelbild Muniqsoft Training

Auswahl  

Komplett Übersicht aller Oracle Tipps

Save Linux shell scripts in Oracle tables 

Oracle
DBA:PL/SQL
RDBMS 21.1:RDBMS 23.1
23.12.24 (MP)
08.04.25(MP)
Shell Script, Linux,

Passende Schulungen zum Thema

Have you always wanted to save your shell scripts in Oracle tables for documentation purposes, even on the client side?

The problem is the many line breaks in the shell script files, so we convert the file to BASE64. 
Since the files can be longer than 32767 characters and thus exceed the maximum length of VARCHAR2 in tables of 4k (or 32K from 12.1 with special setup),

1. Create Funktion :

CREATE OR REPLACE FUNCTION Decode_Base64(
       p_base64 IN CLOB
    ) RETURN CLOB AS
       v_blob       BLOB;
       v_decoded    CLOB;
       v_length     INT;
       v_warning    INT;
       v_offset     INT:= 1;
       v_context    INT:= dbms_lob.default_lang_ctx;
    BEGIN
       -- Convert the Base64 CLOB into a BLOB
       DBMS_LOB.CREATETEMPORARY(v_blob, TRUE);
       DBMS_LOB.WRITE(v_blob, DBMS_LOB.GETLENGTH(p_base64), 1, UTL_RAW.CAST_TO_RAW(p_base64));
       -- Decode the Base64 BLOB
       v_blob := UTL_ENCODE.BASE64_DECODE(v_blob);
       -- Determine the length of the decoded text
       v_length := DBMS_LOB.GETLENGTH(v_blob);
       -- Convert the BLOB to CLOB
       DBMS_LOB.CREATETEMPORARY(v_decoded, TRUE);
       DBMS_LOB.CONVERTTOCLOB(
           DEST_LOB     => v_decoded,
           SRC_BLOB     => v_blob,
           AMOUNT       => v_length,
           DEST_OFFSET  => v_offset,
           SRC_OFFSET   => v_offset,
           blob_csid    => dbms_lob.default_csid,
           lang_context => v_context,
           warning      => v_warning
       );
        
            -- Free resources
       DBMS_LOB.FREETEMPORARY(v_blob);
       RETURN v_decoded;
    END Decode_Base64;
    /

2. Target-Tabelle:

CREATE TABLE script_table (
    id int 
    GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY, 
    content CLOB);

3. At shell level, code the desired file (here: install.sh) in Base64 and save it in variable B64:

export B64=`echo -n \`cat install.sh\` | base64`

4. Enter OS variable in Oracle table and convert back from Base64 to CLOB text:

    sqlplus scott/TIGER@172.24.114.237:1522/pdb1  <<EOF
    set echo off termout off 
    DECLARE
     script_content CLOB := q'^${B64}^';
    BEGIN
     INSERT INTO script_table(content)VALUES(decode_base64(script_content));
     COMMIT;
    END;
    /
    EXIT;
    EOF

So now nothing stands in the way of your version management or audit...
You can get more great tips in one of our 50 Oracle and Postgres courses. We look forward to seeing you ...
 

Weitere Interessante Kurs-Tipps zum Thema

Besuchen Sie uns doch bei einer unsere über 50 Oracle Schulungen in München - Unterhaching, oder verfolgen Sie die Training per Videostreaming bequem vom Home Office oder Büro aus.