penglongli / blog

18 stars 1 forks source link

Bash 下 Etcd 递归删除某个键 #3

Open penglongli opened 7 years ago

penglongli commented 7 years ago

Etcd v2

#!/bin/bash

DIR_NAME=$1
ETCD_DIR_ARR=()

del_key() {
  CURRENT_DIR_NAME=$1

  CURRENT_RESULT=$(etcdctl ls ${CURRENT_DIR_NAME})
  if [ "$CURRENT_RESULT"  = $CURRENT_DIR_NAME ]; then
    etcdctl rm $CURRENT_DIR_NAME
  else
    ETCD_DIR_ARR+=(${CURRENT_DIR_NAME})

    ITEMS=($CURRENT_RESULT)
    for ITEM in ${ITEMS[@]}
    do
      del_key $ITEM
    done
  fi
}

del_dir() {
  ARR_LENGTH=${#ETCD_DIR_ARR[@]}
  for ((i=($ARR_LENGTH-1); i>=0; i--))
  do
    etcdctl rmdir ${ETCD_DIR_ARR[$i]}
  done
}

del_key $DIR_NAME
del_dir

Usage:

./etcd_rm.sh DIR

Etcd v3

wait add