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 * from mytable;"
psql <<EOF > output.txt
\timing
$query
EOF
I want to run that psql command block 5 times and have the results appended to output.txt
. It works fine if I just copy and paste it an additional 4 times, but when I try to put it inside of a for-loop, I get errors. Is there any way to do this?
This is the loop I tired:
#!/bin/bash
query="select * from mytable;"
for (( i=0; i<5; i++ ))
do
psql <<EOF > output.txt
\timing
$query
EOF
done
If I move the final EOF
all the way over to the left, it only executes once, as if the loop wasn't there.