How to execute a psql command within a bash for loop

前端 未结 2 972
Happy的楠姐
Happy的楠姐 2021-01-20 14:11

I want to execute a psql statement within a bash script and output the results to a file. The code I have below works as desired:

#!/bin/bash

query=\"select         


        
2条回答
  •  心在旅途
    2021-01-20 14:35

    You are overwriting the file each time with > inside the loop. You need >> inside or have > outside the loop:

    #!/bin/bash
    
    query="select * from mytable;"
    for (( i=0; i<5; i++ ))
    do
       psql < output.txt
    

    Putting > after done is a little more efficient than >> inside the loop.


    Similar post:

    • Pass values read from a file as input to an SQL query in Oracle

提交回复
热议问题