0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

データベースの名前を配列として取得するシェルスクリプト

Last updated at Posted at 2019-04-13

PostgreSQLのデータベースの名前をshellコマンドから取得する

# ! /bin/bash 

db_host="hoge_host"
db_user="hoge_user"

#
# hogeという名前がついたdbの配列を取得する
# psql -l でdb一覧表示 -x で拡張表示 その際xmlのように表示され NAMEと共にdb名が表示される
# grep Name でdbの名前がある行のみ取得
# awk '/company/{print $3}'でcompanyという文字 print $3で3文字目をprint
# xargsは改行を含む文字列を配列化する
#
db_list=(`psql -h $db_host -U $db_user -d postgres -l -x | grep Name | awk '/hoge/{print $3}'|xargs`)

無理矢理感がすごい。正規表現の部分をワイルドカードなどすれば全てのdbに一致します。もっとスマートな方法があれば教えてください。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?