从 BASH 中的变量中删除 %0D

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20185095/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 08:45:11  来源:igfitidea点击:

Remove %0D from variable in BASH

bashwget

提问by rdshck

I'm trying this:

我正在尝试这个:

token=`curl -I --header "X-Auth-User: [email protected]" --header "X-Auth-Key: XXXXXXXXXXXXXXXXXXXXXX" api.server.com | grep -Fi X-Auth-Token | awk -F" " '{ print }'`

/usr/bin/wget --accept .jpg,.jpeg -p "https://api.server.com/v1/stats/1/graph?callback=jQuery171027000000144289315_1380000003353&mnum=1&res_type=cpu&g_type=6h&graph_type=img&full=1&w=515&h=150&X-Auth-Token=$token" -O "image.jpg" || rm "image.jpg"

But my token result is:

但我的令牌结果是:

https://api.server.com/v1/stats/1/graph?callback=jQuery171027000000144289315_1380000003353&mnum=1&res_type=cpu&g_type=6h&graph_type=img&full=1&w=515&h=150&X-Auth-Token=SERVER_018d8100000000001d1b817f7d58a6%0D

Instead of:

代替:

https://api.server.com/v1/stats/1/graph?callback=jQuery171027000000144289315_1380000003353&mnum=1&res_type=cpu&g_type=6h&graph_type=img&full=1&w=515&h=150&X-Auth-Token=SERVER_018d8100000000001d1b817f7d58a6

How to remove %0D (Carriage return)?

如何删除 %0D(回车)?

回答by that other guy

You can add | tr -d '\r'to your curl pipeline to strip any carriage returns.

您可以添加| tr -d '\r'到 curl 管道以去除任何回车。

回答by crafter

There is a utility called dos2unix. You may have to install it. or use the translate

有一个名为 dos2unix 的实用程序。您可能必须安装它。或使用翻译

tr -d '\r' < input > output

EDIT Found a post that discusses a few options: Remove carriage return in Unix

编辑找到了一篇讨论几个选项的帖子: 在 Unix 中删除回车

回答by Ken Sharp

I solved it by piping to sed. I was already using sedin my pipe so it made sense.

我通过管道解决了它sed。我已经sed在我的管道中使用了,所以这是有道理的。

cmd | sed 's/\r//g'

cmd | sed 's/\r//g'