bash - gcloud 根据时间过滤结果

标签 bash datetime google-cloud-platform gcloud

我正在尝试在 GCP 上列出机器镜像,并根据 CreatimeTimestamp(几个小时/天)过滤出结果。 我正在尝试做这样的事情:

gcloud beta compute machine-images list --sort-by "creationTimestamp" --project=XXXXX --format="table(name,creationTimestamp.date('%Y-%m-%d-%T'))" --filter="CREATION_TIMESTAMP.date('%Y-%m-$d-%H:%M:%S')<'$(date -d '3 hours ago' "+%Y-%m-%d-%H:%M:%S")'"

列出 3 小时前的机器镜像

最佳答案

Update

Thinking more about your question, I think there's probably an easier way than my answer (below). The documentation suggests that you can do a date compare directly. So, perhaps:

WHEN=$(date +%s --date='3 hours ago')
gcloud beta compute machine-images list \
--project=${PROJECT} \
--format="table(name,creationTimestamp)") \
--filter="creationTimestamp.date(\"+%s\")>${WHEN}"

gcloud 过滤器不包含外壳,因此您不能包含例如bash 过滤器中的 date 命令。

我认为(!)做到这一点的方法是格式化输出你想要的结果,然后过滤。

IMAGES=$(gcloud beta compute machine-images list \
--project=${PROJECT} \
--format="csv[no-heading](name,creationTimestamp.date())")

for IMAGE in ${IMAGES}
do
  IFS=, read NAME TIMESTAMP <<< ${IMAGE}
  CREATED=$(date +%s --date=${TIMESTAMP}
  WHEN=$(date +%s --date='3 hours ago')
  if [ ${CREATED} -gt ${WHEN} ]
  then
    printf "Image: %s (Creation: %s)\n" NAME TIMESTAMP
  fi
done

关于bash - gcloud 根据时间过滤结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70489178/

相关文章:

linux - 用于运行命令的 Bash 脚本

Bash,从字符串中用点字符剪切单词

regex - Gnu sed 删除单引号

python - 使用一列 datetime 对象设置两级索引(索引?)为 datetime.date() 和 datetime.time()

google-app-engine - Google App Engine 错误 : <gcloud. app.deploy> INVALID_ARGUMENT : The following quotas were exceeded:BACKEND_SERVICES (quota: 5, 使用:5 + 需要 1)

google-cloud-platform - 如何切换 dbt-BigQuery 运行的 GCP 身份验证帐户

bash - 为什么执行 "docker exec"会杀死我的 SSH session ?

php - 如何在特定日期在 php 中发送?

mysql - 与日期和时间作斗争

kubernetes - 将Cassandra迁移到GCP