如何从bash中的命令输出中检索一些变量



我有一个在管道上执行的命令:

firebase hosting:channel:deploy --only main --project test-project --config firebase.json test

该命令显然抛出了那些输出=>https://github.com/marketplace/actions/deploy-to-firebase-hosting

url部署到的url

过期时间部署的预览URL到期的时间

details_url部署到的单个URL

当我运行它时,它基本上与一起运行

$ firebase hosting:channel:deploy --only main --project test-project --config firebase.json test
=== Deploying to 'test-project'...
i  deploying hosting
i  hosting[test-project]: beginning deploy...
i  hosting[test-project]: found 497 files in dist/apps/main
+  hosting[test-project]: file upload complete
i  hosting[test-project]: finalizing version...
+  hosting[test-project]: version finalized
i  hosting[test-project]: releasing new version...
+  hosting[test-project]: release complete
+  Deploy complete!
Project Console: https://console.firebase.google.com/project/test-project/overview
Hosting URL: https://test-project.web.app
!  hosting:channel: Unable to add channel domain to Firebase Auth. Visit the Firebase Console at https://console.firebase.google.com/project/test-project/authentication/providers

!  hosting:channel: Unable to sync Firebase Auth state.
+  hosting:channel: Channel URL (test-project): https://test-project--test-xj60axa8.web.app [expires 2022-11-01 12:22:04]

我想检索最后一行,将其存储在一个变量中,以便在管道的未来步骤中重用它。

我试着做

- RESULT=$(firebase hosting:channel:deploy --only main --project test-project --config firebase.json test)
- echo "export PREVIEW_LINK=$RESULT" >> set_preview_link.sh

但这将整个命令输出。

有没有办法只得到

hosting:channel: Channel URL (test-project): https://test-project--test-xj60axa8.web.app [expires 2022-11-01 12:22:04]

甚至只是https://test-project--test-xj60axa8.web.app

tail对于获取输出的最后一行非常有用。

RESULT=$(firebase hosting:channel:deploy --only main --project test-project --config firebase.json test | tail -1)
echo "$RESULT"

具体获取URL-根据可能的输出,您可以尝试使用类似awkcut的方法提取列。正则表达式也可以。

RESULT=$(firebase hosting:channel:deploy --only main --project test-project --config firebase.json test | tail -1)
url_regex='(https://[[:alnum:].-]+)'
[[ "$RESULT" =~ $url_regex ]] && echo "${BASH_REMATCH[1]}"

如果您的grep支持-P标志。

RESULT="$(firebase hosting:channel:deploy ... | grep -Po '(?<=Channel URL (test-project): )'.*'(?= [)')"

检查RESULT的内容

declare -p RESULT

最新更新