LXDスナップショット名配列の取得

LXDスナップショット名配列の取得

私はこれに非常に慣れていません。誰かがスナップショット名リストから配列を取得するgrep方法を教えてもらえますかbashメモ:名前のみ)作成するときlxc info mycontainer

私の現在の結果は次のとおりです。

root@hosting:~/LXC-Commander# lxc info mycontainer --verbose
Name: mycontainer
Remote: unix:/var/lib/lxd/unix.socket
Architecture: x86_64
Created: 2017/05/01 21:27 UTC
Status: Running
Type: persistent
Profiles: mine
Pid: 23304
Ips:
  eth0: inet    10.58.122.150   vethDRS01G
  eth0: inet6   fd9b:16e1:3513:f396:216:3eff:feb1:c997  vethDRS01G
  eth0: inet6   fe80::216:3eff:feb1:c997        vethDRS01G
  lo:   inet    127.0.0.1
  lo:   inet6   ::1
Resources:
  Processes: 1324
  Memory usage:
    Memory (current): 306.63MB
    Memory (peak): 541.42MB
  Network usage:
    eth0:
      Bytes received: 289.16kB
      Bytes sent: 881.73kB
      Packets received: 692
      Packets sent: 651
    lo:
      Bytes received: 1.51MB
      Bytes sent: 1.51MB
      Packets received: 740
      Packets sent: 740
Snapshots:
  2017-04-29-mycontainer (taken at 2017/04/29 21:54 UTC) (stateless)
  2017-04-30-mycontainer (taken at 2017/04/30 21:54 UTC) (stateless)
  2017-05-01-mycontainer (taken at 2017/05/01 21:54 UTC) (stateless)

私の最終目標は、次の配列を含めることです。2017-04-29-mycontainer 2017-04-30-mycontainer 2017-05-01-mycontainer

ベストアンサー1

lxc list --format=json利用可能なすべてのさまざまなコンテナに関する多くの情報を含むJSONドキュメントを受け取ります。

lxc list mycontainer --format=json名前は文字列で始まるコンテナに制限されますmycontainer'mycontainer$'正確な一致のため)。

JSONを解析するのは通常、ほとんど自由形式のテキスト文書を解析するよりも安全です。

スナップショット名の抽出使用jq:

$ lxc list mycontainer --format=json | jq -r '.[].snapshots[].name'

これにより、次のようなリストが提供されます。

2017-04-29-mycontainer
2017-04-30-mycontainer
2017-05-01-mycontainer

配列に入れるにはbash

snaps=( $( lxc list mycontainer --format=json | jq -r '.[].snapshots[].name' ) )

これにより、*?[スナップショット名にシェル固有の文字()を含めると、ファイル名のグロービングが発生します。set -fコマンドset +fの前後に発生するのを防ぐことができます。

スナップショットだけを繰り返す場合:

lxc list mycontainer --format=json | jq -r '.[].snapshots[].name' |
while read snap; do
   # do something with "$snap"
done

おすすめ記事