使用 grep 从详细调用中删除并保存在 shell 脚本函数中

发布于 2025-01-12 15:57:11 字数 689 浏览 2 评论 0原文

我有一个curl put请求,我可以使用一些unix命令来查看stdout和stderr。

如果我添加行 2>&1 | grep -v "Authorization" 在我的curl请求结束时,我可以在CLI中看到我的curl的详细输出,减去以“Authorization”开头的行

我尝试创建一个使用上述命令运行函数,但是当我在curl请求末尾调用该函数时(如下所示),它不再删除“授权”行。

function remove_item_from_verbose { 
  2>&1 | grep -v "Authorization" 
}

echo -e "\n +++ Creating '$TENANT' tenant:\n"
# Create tenant
curl -L -X PUT "http://localhost:$HOST_PORT/admin/v2/tenants/$TENANT" \
    --verbose \
    --header 'Content-Type: application/json' \
    --header "Authorization: Bearer $AUTHORIZATION" \
    --data-raw "{\"allowedClusters\": [\"$CLUSTER\"]}" remove_item_from_verbose

I have a curl put request that I can use some unix command to see the stdout and stderr from.

If I add the line 2>&1 | grep -v "Authorization" At the end of my curl request, I can see the verbose output from my curl in my CLI minus a line that starts with "Authorization"

I tried creating a function with the above command, but when I call that function at the end of my curl request (like below) it no longer removes the "Authorization" line.

function remove_item_from_verbose { 
  2>&1 | grep -v "Authorization" 
}

echo -e "\n +++ Creating '$TENANT' tenant:\n"
# Create tenant
curl -L -X PUT "http://localhost:$HOST_PORT/admin/v2/tenants/$TENANT" \
    --verbose \
    --header 'Content-Type: application/json' \
    --header "Authorization: Bearer $AUTHORIZATION" \
    --data-raw "{\"allowedClusters\": [\"$CLUSTER\"]}" remove_item_from_verbose

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦里南柯 2025-01-19 15:57:12

据我所知,函数确实是这样工作的。您正在尝试为您的命令添加额外的详细信息。您可以这样做:

#!/bin/bash

remove_item_from_verbose="2>&1 | grep -v \"Authorization\""

echo -e "\n +++ Creating '$TENANT' tenant:\n"

# Create tenant
eval curl -L -X PUT "http://localhost:$HOST_PORT/admin/v2/tenants/$TENANT" \
    --verbose \
    --header 'Content-Type: application/json' \
    --header "Authorization: Bearer $AUTHORIZATION" \
    --data-raw "{\"allowedClusters\": [\"$CLUSTER\"]}" "$remove_item_from_verbose"

像这样,$remove_item_from_verbose 成为命令的后缀。
注意在curl前面添加了eval,要求将此后缀视为命令的一部分。

AFAIK functions do work like that. What you are trying to so is add extra details to your command. You can do it like that:

#!/bin/bash

remove_item_from_verbose="2>&1 | grep -v \"Authorization\""

echo -e "\n +++ Creating '$TENANT' tenant:\n"

# Create tenant
eval curl -L -X PUT "http://localhost:$HOST_PORT/admin/v2/tenants/$TENANT" \
    --verbose \
    --header 'Content-Type: application/json' \
    --header "Authorization: Bearer $AUTHORIZATION" \
    --data-raw "{\"allowedClusters\": [\"$CLUSTER\"]}" "$remove_item_from_verbose"

Like this, $remove_item_from_verbose becomes a suffix of your command.
Note the addition of eval in front of curl, it is required to have this suffix treated as part of the command.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文