Pergunta

I have a select statement inside a bash script that returns the latest date in the DB. I run this query 4 times so I want to define it just once and assing the text to a variable.

#!/bin/bash

    linux commands;
    database_date=$(sqlplus -s/nolog $USER/$USER@BRMDPP <<END
                 set pagesize 0 feedback off verify off heading off echo off;
                 SELECT ... 
                 exit;
END
)


    commands that change the database date;



     last_date=$(sqlplus -s/nolog $USER/$USER@BRMDPP <<END
                 set pagesize 0 feedback off verify off heading off echo off;
                 SELECT ... 
                 exit;
END
)

commands that change the database date;

How can I store this big string $(sqlplus ... into one variable and use it again? Thank you

Foi útil?

Solução

One way would be to make use of a function:

foo() {
sqlplus -s/nolog $USER/$USER@BRMDPP <<END
                 set pagesize 0 feedback off verify off heading off echo off;
                 SELECT ... 
                 exit;
END
}

and later invoke it by saying:

value=$(foo)

In order to get the value returned by the function, say echo "$value" (note that quoting variables is important).

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top