Find the answer to your Linux question:
Results 1 to 4 of 4
Greetings, I am working on a menu driven bash script. I need to import text for sql queries into the main script and modify some variable data (dates) to pull ...
  1. #1
    Just Joined!
    Join Date
    Jun 2008
    Location
    Grand Rapids, MI
    Posts
    2

    Inserting variables into imported text

    Greetings,

    I am working on a menu driven bash script.

    I need to import text for sql queries into the main script and modify some variable data (dates) to pull the information I want.

    I get the date form a user input read prompt. I can pass the variable around.

    But I need to know how to use that variable inside the imported text.

    I am pulling the text in using the following:

    while read LINE
    do
    echo $LINE
    done < filename

    Here is an example of one of the scripts.

    select stno, cusid, invno, adjamt from invheader
    where trantype='A' AND VOID=’N’ and invdate>'X/26/2008’

    I want to replace the invdate with a variable date.

    Suggestions?

    Thanks

    Larry

  2. #2
    Linux Engineer khafa's Avatar
    Join Date
    Apr 2008
    Location
    Tokyo, Japan
    Posts
    858
    if you tell us how you are going to import the sql scripts and what do you do with them afterwards it would help us. are the sql scripts in a file? do you write back the result after the change to the file?
    Linux and me it's a love story

  3. #3
    Just Joined!
    Join Date
    Jun 2008
    Location
    Grand Rapids, MI
    Posts
    2
    The sql are in files that I want to load and run in memory. I have about 130 different statements I run every month... takes all day to do it by cut-and-paste.

    I could rewrite them back to text files and have another function call that rewritten file... seems wasteful.

    I have a group of variables:
    $prev_date
    $curr_date
    $curr_month
    $fin_stmt_date

    I would like to take the sql statement..
    select stno, cusid, invno, adjamt from invheader where trantype='A' AND VOID=’N’ and invdate>'X/26/2008’

    and convert it to this and run it in one process..
    select stno, cusid, invno, adjamt from invheader where trantype='A' AND VOID=’N’ and invdate>$prev_date

    I already do some minor checking using isqf-fb to connect to our database and pull info.

    I just need to know how to pull a test file in, manipulate it and use it. The date are not always in the same place in the sql queries.

    Hope this helps

  4. #4
    Linux Guru
    Join Date
    Nov 2007
    Location
    Córdoba (Spain)
    Posts
    1,513
    If you use "invdate" as a placeholder for $curr_date (or any other variable, it's just an example), you could just use sed to make string substitution. Or, you could as well use the bash string mangling capabilities, examples:

    Code:
    sed -e 's/invdate/'$curr_date'/g' filename
    To substitute all the ocurrences in the file. Or this:

    Code:
    LINE=$(echo "$LINE | sed -e 's/invdate/'$curr_date'/'
    Which in bash could also be done:

    Code:
    LINE=${LINE/invdate/$curr_date}
    This form is much cleaner, but, as said, this will only work on bash, while the sed solution is portable and should work regardless of the shell you use.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...