"echo"ing a psql command in a bash script

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ennio-Sr

    "echo"ing a psql command in a bash script

    Hi all!
    I'm writing a script that presents the user with a numbered lines
    menu, each line corresponding to a <case n> which executes a psql
    command. As the psql-commands are very similar to each other (all of
    them have the structure:

    1.- psql mydb -x -c "SELECT * FROM tb_nm WHERE col_nm LIKE '%$k_r%'" )

    I thought it was possible to shorten it, initializing a str with said
    command at the beginning of the script and limiting the various case
    lines to defining the col_nm and the $k_r to be searched, i.e.:

    2. -
    (once for all):
    cmnd=echo psql mydb -x -c "SELECT * FROM tb_nm WHERE $col_nm LIKE '%$k_r%'"

    (in each <case n>):
    col_nm="....."
    echo $cmnd
    ---------------------------
    The point is that:
    when I test command "1", the query result appears immediately on the
    screen and disappears (leaving room to the script menu), when I press
    "q"; i.e., the environment remains that of psql until I press "q", which
    seems to be a correct behaviour;

    whereas, testing command "2", first appears the menu again (as if the
    query had been unsuccessfull) and then (after pressing Enter - which
    normally would cause exiting the script) the query result shows.

    Could somebody throw some light on this issue?
    Thanks for your attention.
    Ennio.

    --
    [Perche' usare Win$ozz (dico io) se ..."anche uno sciocco sa farlo. \\?//
    Fa' qualche cosa di cui non sei capace!" (diceva Henry Miller) ] (°|°)
    [Why to use Win$ozz (I say) if ... "even a fool can do that. )=(
    Do something you aren't good at!" (used to say Henry Miller) ]

    ---------------------------(end of broadcast)---------------------------
    TIP 3: if posting/reading through Usenet, please send an appropriate
    subscribe-nomail command to majordomo@postg resql.org so that your
    message can get through to the mailing list cleanly

  • Jim Wilson

    #2
    Re: &quot;echo&quot ;ing a psql command in a bash script

    Ennio-Sr said:
    [color=blue]
    > (once for all):
    > cmnd=echo psql mydb -x -c "SELECT * FROM tb_nm WHERE $col_nm LIKE '%$k_r%'"[/color]

    That will be executed immediately, and give you an error.
    [color=blue]
    > (in each <case n>):
    > col_nm="....."
    > echo $cmnd[/color]

    I'm surprised you get anything. AFAIK this should parse the files in your
    directory into column names. Maybe you are doing something slightly different?

    Rather than setting the $cmnd variable, try using something like this:

    echo mydb -x -c \"SELECT "*" FROM tb_nm WHERE $col_nm LIKE \'%$k_r%\'\" |
    xargs psql

    I'm not sure where you are storing your column names, but if you can pipe it
    as a list into the above you won't need to rum a loop in your script. There
    are a thousand ways to do this, but most important is remember to quote the
    "*" so the shell doesn't parse it. BTW You did not say, so I'm also assuming
    you are using "bash" or "sh" since that's what this looks like. :-)

    Best,

    Jim


    ---------------------------(end of broadcast)---------------------------
    TIP 3: if posting/reading through Usenet, please send an appropriate
    subscribe-nomail command to majordomo@postg resql.org so that your
    message can get through to the mailing list cleanly

    Comment

    Working...