Storing oracle query results into bash variable The Next CEO of Stack OverflowGet the source directory of a Bash script from within the script itselfHow to check if a string contains a substring in BashHow to check if a program exists from a Bash script?How do I tell if a regular file does not exist in Bash?Extract filename and extension in BashLooping through the content of a file in BashHow to check if a variable is set in Bash?How to concatenate string variables in BashEcho newline in Bash prints literal nLoop through an array of strings in Bash?

Raspberry pi 3 B with Ubuntu 18.04 server arm64: what pi version

Can a PhD from a non-TU9 German university become a professor in a TU9 university?

How to coordinate airplane tickets?

What steps are necessary to read a Modern SSD in Medieval Europe?

How to pronounce fünf in 45

How to show a landlord what we have in savings?

pgfplots: How to draw a tangent graph below two others?

Is it reasonable to ask other researchers to send me their previous grant applications?

Is the offspring between a demon and a celestial possible? If so what is it called and is it in a book somewhere?

Early programmable calculators with RS-232

Is it possible to create a QR code using text?

Mathematica command that allows it to read my intentions

Which acid/base does a strong base/acid react when added to a buffer solution?

How to find if SQL server backup is encrypted with TDE without restoring the backup

How can a day be of 24 hours?

What is the difference between 'contrib' and 'non-free' packages repositories?

How do I secure a TV wall mount?

Read/write a pipe-delimited file line by line with some simple text manipulation

Could you use a laser beam as a modulated carrier wave for radio signal?

What did the word "leisure" mean in late 18th Century usage?

Ising model simulation

Does int main() need a declaration on C++?

Is there a rule of thumb for determining the amount one should accept for a settlement offer?

Free fall ellipse or parabola?



Storing oracle query results into bash variable



The Next CEO of Stack OverflowGet the source directory of a Bash script from within the script itselfHow to check if a string contains a substring in BashHow to check if a program exists from a Bash script?How do I tell if a regular file does not exist in Bash?Extract filename and extension in BashLooping through the content of a file in BashHow to check if a variable is set in Bash?How to concatenate string variables in BashEcho newline in Bash prints literal nLoop through an array of strings in Bash?










1















declare -a result=`$ORACLE_HOME/bin/sqlplus -silent $DBUSER/$DBPASSWORD@$DB << EOF $SQLPLUSOPTIONS $roam_query exit; EOF`


I am trying to pull data from an oracle database and populate a bash variable. The select query works however it returns multiple rows and those rows are returned as a long continuous string. I want to capture each row from the database in an array index for example:



index[0] = row 1 information
index[1] = row 2 information


Please help. All suggestions are appreciated. I checked all documentation without no luck. Thank you. I am using solaris unix










share|improve this question
























  • Stack overflow was not allowing me to enter the `` but I know they are supposed to be there so that is not the issue.

    – Kevoy Walters
    Mar 8 at 18:26











  • You are assigning the entire result to the first element of the array. A first pass at doing what you want would be declare -a results=( $($ORACLE_HOME/bin/sqlplus ...) ).

    – chepner
    Mar 8 at 18:33











  • That has its own risks, though, as it subjects the result to word-splitting and pathname expansion. (Most importantly, you'll probably end up with one array entry per field per row, not just one entry per row.)

    – chepner
    Mar 8 at 18:35















1















declare -a result=`$ORACLE_HOME/bin/sqlplus -silent $DBUSER/$DBPASSWORD@$DB << EOF $SQLPLUSOPTIONS $roam_query exit; EOF`


I am trying to pull data from an oracle database and populate a bash variable. The select query works however it returns multiple rows and those rows are returned as a long continuous string. I want to capture each row from the database in an array index for example:



index[0] = row 1 information
index[1] = row 2 information


Please help. All suggestions are appreciated. I checked all documentation without no luck. Thank you. I am using solaris unix










share|improve this question
























  • Stack overflow was not allowing me to enter the `` but I know they are supposed to be there so that is not the issue.

    – Kevoy Walters
    Mar 8 at 18:26











  • You are assigning the entire result to the first element of the array. A first pass at doing what you want would be declare -a results=( $($ORACLE_HOME/bin/sqlplus ...) ).

    – chepner
    Mar 8 at 18:33











  • That has its own risks, though, as it subjects the result to word-splitting and pathname expansion. (Most importantly, you'll probably end up with one array entry per field per row, not just one entry per row.)

    – chepner
    Mar 8 at 18:35













1












1








1








declare -a result=`$ORACLE_HOME/bin/sqlplus -silent $DBUSER/$DBPASSWORD@$DB << EOF $SQLPLUSOPTIONS $roam_query exit; EOF`


I am trying to pull data from an oracle database and populate a bash variable. The select query works however it returns multiple rows and those rows are returned as a long continuous string. I want to capture each row from the database in an array index for example:



index[0] = row 1 information
index[1] = row 2 information


Please help. All suggestions are appreciated. I checked all documentation without no luck. Thank you. I am using solaris unix










share|improve this question
















declare -a result=`$ORACLE_HOME/bin/sqlplus -silent $DBUSER/$DBPASSWORD@$DB << EOF $SQLPLUSOPTIONS $roam_query exit; EOF`


I am trying to pull data from an oracle database and populate a bash variable. The select query works however it returns multiple rows and those rows are returned as a long continuous string. I want to capture each row from the database in an array index for example:



index[0] = row 1 information
index[1] = row 2 information


Please help. All suggestions are appreciated. I checked all documentation without no luck. Thank you. I am using solaris unix







linux bash oracle unix solaris






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 8 at 18:33









that other guy

74.9k886124




74.9k886124










asked Mar 8 at 18:25









Kevoy WaltersKevoy Walters

206




206












  • Stack overflow was not allowing me to enter the `` but I know they are supposed to be there so that is not the issue.

    – Kevoy Walters
    Mar 8 at 18:26











  • You are assigning the entire result to the first element of the array. A first pass at doing what you want would be declare -a results=( $($ORACLE_HOME/bin/sqlplus ...) ).

    – chepner
    Mar 8 at 18:33











  • That has its own risks, though, as it subjects the result to word-splitting and pathname expansion. (Most importantly, you'll probably end up with one array entry per field per row, not just one entry per row.)

    – chepner
    Mar 8 at 18:35

















  • Stack overflow was not allowing me to enter the `` but I know they are supposed to be there so that is not the issue.

    – Kevoy Walters
    Mar 8 at 18:26











  • You are assigning the entire result to the first element of the array. A first pass at doing what you want would be declare -a results=( $($ORACLE_HOME/bin/sqlplus ...) ).

    – chepner
    Mar 8 at 18:33











  • That has its own risks, though, as it subjects the result to word-splitting and pathname expansion. (Most importantly, you'll probably end up with one array entry per field per row, not just one entry per row.)

    – chepner
    Mar 8 at 18:35
















Stack overflow was not allowing me to enter the `` but I know they are supposed to be there so that is not the issue.

– Kevoy Walters
Mar 8 at 18:26





Stack overflow was not allowing me to enter the `` but I know they are supposed to be there so that is not the issue.

– Kevoy Walters
Mar 8 at 18:26













You are assigning the entire result to the first element of the array. A first pass at doing what you want would be declare -a results=( $($ORACLE_HOME/bin/sqlplus ...) ).

– chepner
Mar 8 at 18:33





You are assigning the entire result to the first element of the array. A first pass at doing what you want would be declare -a results=( $($ORACLE_HOME/bin/sqlplus ...) ).

– chepner
Mar 8 at 18:33













That has its own risks, though, as it subjects the result to word-splitting and pathname expansion. (Most importantly, you'll probably end up with one array entry per field per row, not just one entry per row.)

– chepner
Mar 8 at 18:35





That has its own risks, though, as it subjects the result to word-splitting and pathname expansion. (Most importantly, you'll probably end up with one array entry per field per row, not just one entry per row.)

– chepner
Mar 8 at 18:35












1 Answer
1






active

oldest

votes


















0














If you have bash version 4, you can use the readarray -t command to do this. Any vaguely recent linux should have bash v4, but I don't know about Solaris.



BTW, I'd also recommend putting double-quotes around variable references (e.g. "$DBUSER/$DBPASSWORD@$DB" instead of just $DBUSER/$DBPASSWORD@$DB) (except in here-documents), using $( ) instead of backticks, and using lower- or mixed-case variable names (there are a bunch of all-caps names with special meanings, and if you use one of those by accident, weird things can happen).



I'm not sure I have the here-document (the SQL commands) right, but here's roughly how I'd do it:



readarray -t result < <("$oracle_home/bin/sqlplus" -silent "$dbuser/$dbpassword@$db" << EOF
$sqlplusoptions $roam_query
exit;
EOF
)





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55068957%2fstoring-oracle-query-results-into-bash-variable%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    If you have bash version 4, you can use the readarray -t command to do this. Any vaguely recent linux should have bash v4, but I don't know about Solaris.



    BTW, I'd also recommend putting double-quotes around variable references (e.g. "$DBUSER/$DBPASSWORD@$DB" instead of just $DBUSER/$DBPASSWORD@$DB) (except in here-documents), using $( ) instead of backticks, and using lower- or mixed-case variable names (there are a bunch of all-caps names with special meanings, and if you use one of those by accident, weird things can happen).



    I'm not sure I have the here-document (the SQL commands) right, but here's roughly how I'd do it:



    readarray -t result < <("$oracle_home/bin/sqlplus" -silent "$dbuser/$dbpassword@$db" << EOF
    $sqlplusoptions $roam_query
    exit;
    EOF
    )





    share|improve this answer



























      0














      If you have bash version 4, you can use the readarray -t command to do this. Any vaguely recent linux should have bash v4, but I don't know about Solaris.



      BTW, I'd also recommend putting double-quotes around variable references (e.g. "$DBUSER/$DBPASSWORD@$DB" instead of just $DBUSER/$DBPASSWORD@$DB) (except in here-documents), using $( ) instead of backticks, and using lower- or mixed-case variable names (there are a bunch of all-caps names with special meanings, and if you use one of those by accident, weird things can happen).



      I'm not sure I have the here-document (the SQL commands) right, but here's roughly how I'd do it:



      readarray -t result < <("$oracle_home/bin/sqlplus" -silent "$dbuser/$dbpassword@$db" << EOF
      $sqlplusoptions $roam_query
      exit;
      EOF
      )





      share|improve this answer

























        0












        0








        0







        If you have bash version 4, you can use the readarray -t command to do this. Any vaguely recent linux should have bash v4, but I don't know about Solaris.



        BTW, I'd also recommend putting double-quotes around variable references (e.g. "$DBUSER/$DBPASSWORD@$DB" instead of just $DBUSER/$DBPASSWORD@$DB) (except in here-documents), using $( ) instead of backticks, and using lower- or mixed-case variable names (there are a bunch of all-caps names with special meanings, and if you use one of those by accident, weird things can happen).



        I'm not sure I have the here-document (the SQL commands) right, but here's roughly how I'd do it:



        readarray -t result < <("$oracle_home/bin/sqlplus" -silent "$dbuser/$dbpassword@$db" << EOF
        $sqlplusoptions $roam_query
        exit;
        EOF
        )





        share|improve this answer













        If you have bash version 4, you can use the readarray -t command to do this. Any vaguely recent linux should have bash v4, but I don't know about Solaris.



        BTW, I'd also recommend putting double-quotes around variable references (e.g. "$DBUSER/$DBPASSWORD@$DB" instead of just $DBUSER/$DBPASSWORD@$DB) (except in here-documents), using $( ) instead of backticks, and using lower- or mixed-case variable names (there are a bunch of all-caps names with special meanings, and if you use one of those by accident, weird things can happen).



        I'm not sure I have the here-document (the SQL commands) right, but here's roughly how I'd do it:



        readarray -t result < <("$oracle_home/bin/sqlplus" -silent "$dbuser/$dbpassword@$db" << EOF
        $sqlplusoptions $roam_query
        exit;
        EOF
        )






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 8 at 19:46









        Gordon DavissonGordon Davisson

        71k97894




        71k97894





























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55068957%2fstoring-oracle-query-results-into-bash-variable%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Identity Server 4 is not redirecting to Angular app after login2019 Community Moderator ElectionIdentity Server 4 and dockerIdentityserver implicit flow unauthorized_clientIdentityServer Hybrid Flow - Access Token is null after user successful loginIdentity Server to MVC client : Page Redirect After loginLogin with Steam OpenId(oidc-client-js)Identity Server 4+.NET Core 2.0 + IdentityIdentityServer4 post-login redirect not working in Edge browserCall to IdentityServer4 generates System.NullReferenceException: Object reference not set to an instance of an objectIdentityServer4 without HTTPS not workingHow to get Authorization code from identity server without login form

            2005 Ahvaz unrest Contents Background Causes Casualties Aftermath See also References Navigation menue"At Least 10 Are Killed by Bombs in Iran""Iran"Archived"Arab-Iranians in Iran to make April 15 'Day of Fury'"State of Mind, State of Order: Reactions to Ethnic Unrest in the Islamic Republic of Iran.10.1111/j.1754-9469.2008.00028.x"Iran hangs Arab separatists"Iran Overview from ArchivedConstitution of the Islamic Republic of Iran"Tehran puzzled by forged 'riots' letter""Iran and its minorities: Down in the second class""Iran: Handling Of Ahvaz Unrest Could End With Televised Confessions""Bombings Rock Iran Ahead of Election""Five die in Iran ethnic clashes""Iran: Need for restraint as anniversary of unrest in Khuzestan approaches"Archived"Iranian Sunni protesters killed in clashes with security forces"Archived

            Can't initialize raids on a new ASUS Prime B360M-A motherboard2019 Community Moderator ElectionSimilar to RAID config yet more like mirroring solution?Can't get motherboard serial numberWhy does the BIOS entry point start with a WBINVD instruction?UEFI performance Asus Maximus V Extreme