This article is half-done without your Comment! *** Please share your thoughts via Comment ***
Now a day, I am developing different Linux shell scripts to monitor the PostgreSQL database server. Later, I will share those shell scripts.
But before that, I want to share few basic things which we can embed in any one of our Shell scripts.
In this post, I am sharing a small shell script to iterate the result of psql SELECT using RECORD ARRAY and WHILE LOOP.
Using that, we can do further processing and validation on each record.
Script to iterate the table result:
1 2 3 4 5 6 7 8 9 10 11 |
#!/bin/bash DB_NAME=$1 psql -d ${DB_NAME} -At -c "select rno, studname, studclass from tbl_students" \ | while read -a Record ; do rno=${Record[0]} studname=${Record[1]} studclass=${Record[2]} echo "${rno} ${studname} ${studclass}" done |
1 2 3 4 5 6 7 8 9 10 11 12 |
rno | studname -----+----------+----------- 1 | Anvesh 2 | Neevan 3 | Toby 5 | Roy 4 | Jenny 6 | Kaviy 7 | Martin 8 | Laxmi 9 | Nion (9 rows) |