Re: shell script to populate array values

Поиск
Список
Период
Сортировка
От SCassidy@overlandstorage.com
Тема Re: shell script to populate array values
Дата
Msg-id OFD82D3EB0.0025F1FB-ON88257241.007E2D8A-88257241.007EE218@overlandstorage.com
обсуждение исходный текст
Ответ на shell script to populate array values  (Paul Silveira <plabrh1@gmail.com>)
Список pgsql-general
You should be able to use something like this in a bash script:

psql -U postgres -hMYSERVER --quiet --no-align --field-separator ' ' -t -c
"SELECT servername,instanceport from server where serverclass = 3 and
isactive = 'True'" Admin |
while  read -a SVRDATA ;do
  echo "name: ${SVRDATA[0]}  port: ${SVRDATA[1]}"
  # array item SVRDATA[0] is the name,  SVRDATA[1] is the port
  #do whatever you want with the name and port inside the loop
done

Notice that you are piping the output of the psql command into the while
loop.  Also, the field-separator is a space, which makes 'read' happy.

Susan Cassidy




             Paul Silveira
             <plabrh1@gmail.co
             m>                                                         To
             Sent by:                  pgsql-general@postgresql.org
             pgsql-general-own                                          cc
             er@postgresql.org
                                                                   Subject
                                       [GENERAL] shell script to populate
             12/11/2006 12:42          array values
             PM

                                      |-------------------|
                                      | [ ] Expand Groups |
                                      |-------------------|











Hello,

I would like to create a shell script that would populate two variables
with
the return of a SELECT statement that would return two attributes...

For example...

#!/bin/bash
SERVER_NAMES=`psql Admin -Upostgres -hMYSERVER -t -c"SELECT servername,
instanceport from server where serverclass = 3 and isactive = 'True' ";`


As you can see, I'm returning the servername "and" the instanceport from
the
server table.  This will later allow me to create psql commands to connect
to each server dynamically.

I had the script working correctly when I was just running it for the
server
name as below...
#!/bin/bash
SERVER_NAMES=`psql Admin -Upostgres -hMYSERVER -t -c"SELECT servername from
server where serverclass = 3 and isactive = 'True' ";`


Does anyone know the easiest way to get both values out of some variables
that I could set?

Later in the script, I'm creating a for loop and iterating through the
server names and would like to build connection strings dynamically with
the
results from the select string...



             for i in $SERVER_NAMES
             do
                  psql -Upostgres -h$i -p$i -A -t -c"SELECT '$i',
'$BATCHTIME', name,
setting, category, short_desc, context, vartype, source, min_val, max_val
FROM pg_settings;" | psql Admin -Upostgres -hMYSERVER -t -c"COPY
serverconfigsetting FROM STDIN WITH DELIMITER '|' NULL AS '';"
                  echo "Done with $i"
             done


As you can see I have "-h$i -p$i" in the script for the host and port.
Again the script worked fine when I just had the -h@i in there...

I know that the current forloop is incorrect specifiying the $i twice but I
just put that in there to show an example of what I was hoping to do...  It
would probably be more accessing the array value like -h$i[0:0] -p$i[0:1]
in
pseudo code for accessing array values.

Thanks in advance,

Paul





--
View this message in context:
http://www.nabble.com/shell-script-to-populate-array-values-tf2796502.html#a7802638

Sent from the PostgreSQL - general mailing list archive at Nabble.com.


---------------------------(end of broadcast)---------------------------
TIP 6: explain analyze is your friend




----------------------------------------------------------------------------------------------
Simply protected storage solutions ensure that your information is
automatically safe, readily available and always there, visit us at http://www.overlandstorage.com
----------------------------------------------------------------------------------------------


В списке pgsql-general по дате отправления:

Предыдущее
От: "philip johnson"
Дата:
Сообщение: Re: tsearch2 and pdf files
Следующее
От: Scott Marlowe
Дата:
Сообщение: Re: forcing compression of text field