卷曲(和/)或不和谐无法正确处理(使用bash)

发布于 2025-01-17 15:16:59 字数 1397 浏览 3 评论 0 原文

我正在尝试使用网络钩子和curl 自动将游戏服务器中的玩家列出到Discord。

一些变量是:

$PREPLAY 定义为 PREPLAY=__Currently Connected:__ 这会添加带下划线的文本

$MESSAGE 定义为 MESSAGE="当前在线计数:\"$NOWCOUNT\"(这有效,但必须针对 stackoverflow 标记进行编辑)

由其他脚本定义的 $NOWCOUNT(这也有效)

$PLAYERS (玩家列表) - 我稍后会详细介绍

我正在使用的行是:

curl -H "Content-Type: application/json" - X POST -d "{\"嵌入\": [{ \"颜色\": \"45044\", \"描述\": \"$PREPLAY$PLAYERS\", \"标题\": \"$信息\” }]}" $URL

当我使用:

PLAYERS=$(cat /tmp/players.list | sed '/Players linked/q' | egrep -v '^$' | egrep -v " ^LOG" | sed -e 's/^-/\\\\\\n/')

Discord 中的输出为

注意多余的

注意多余的 "\"

或者如果,我从上面的变量赋值中删除ONE“\”,将其变为:

PLAYERS=$(cat /tmp/players.list | sed '/玩家已连接/q' | egrep -v '^$'| egrep -v“^LOG”| sed -e 's/^-/\\\\\n/')

我收到 JSON 错误:

在此处输入图像描述

好的,删除两个“/” s 和新行只是不起作用:

在此处输入图像描述

I'm trying to automate listing players in a game server to Discord using webhooks and curl.

Some variables are:

$PREPLAY defined as PREPLAY=__Currently Connected:__ this adds the text underlined

$MESSAGE defined as MESSAGE="Current Online Count: \"$NOWCOUNT\" (this works, but had to edit for stackoverflow markup)

$NOWCOUNT defined by other script (This also works)

$PLAYERS (the list of players) - I'll get more into this in a second

The line I'm using is:

curl -H "Content-Type: application/json" -X POST -d "{\"embeds\": [{ \"color\": \"45044\", \"description\": \"$PREPLAY$PLAYERS\", \"title\": \"$MESSAGE\" }]}" $URL

when I use:

PLAYERS=$(cat /tmp/players.list | sed '/Players connected/q' | egrep -v '^$' | egrep -v "^LOG" | sed -e 's/^-/\\\\\\n/')

The output in Discord comes out as

Notice the extra ""

Notice the extra "\"

Or if, I remove ONE "\" from the variable assignment above to be:

PLAYERS=$(cat /tmp/players.list | sed '/Players connected/q' | egrep -v '^$' | egrep -v "^LOG" | sed -e 's/^-/\\\\\n/')

I get a JSON error:

enter image description here

OK, removing TWO "/"s and the new-lines just don't work:

enter image description here

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

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

发布评论

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

评论(1

剧终人散尽 2025-01-24 15:17:00

我了解玩家变量生成的问题。

建议使用 set -x set +x 进行调试以检查变量:

set -x # enable debug trace
PLAYERS=$(cat /tmp/players.list | sed '/Players connected/q' | egrep -v '^

命令逐步:

PLAYERS1=$(sed '/Players connected/q' /tmp/players.list)
echo "PLAYERS1=$PLAYERS1" # debug trace

建议尝试测试 players connected/

PLAYERS2=$(sed '/Players connected/q' /tmp/players.list| egrep -v '^([[:space:]]*$|LOG)')
echo "PLAYERS2=$PLAYERS2" # debug trace

这应该打印/tmp/players.list list lines,直到找到Regexp Pattern /players connect/

滤除空线,并以 log 开头的行

PLAYERS3=$(sed '/Players connected/q' /tmp/players.list| egrep -v '^([[:space:]]*$|LOG)'| sed -e 's|^-|\\\\\n|')
echo "PLAYERS3=$PLAYERS3" # debug trace

应该打印/tmp/players.list所有行,直到找到Regexp Pattern /players connected/

log 开始过滤空线,

并以 - 替换为 \ code> \\ ,然后是新行。

例如:

  line 1
  -line 2

将变换:

  line 1
  \\
  line 2

如果要将行与 \\ n contine contect:

PLAYERS4=$(sed '/Players connected/q' /tmp/players.list|egrep -v '^([[:space:]]*$|LOG)'| sed -z 's|\n-|\\\\n|')
echo "PLAYERS4=$PLAYERS4" # debug trace

例如:

  line 1
  -line 2

will tr​​ansform:

  line 1\\nline 2

可以简化/折叠 sed 命令和 egrep 命令命令到单个 awk 命令中。

PLAYERS5=$(awk '!/^([[:space:]]*$|LOG)/{sub("-","\\\\n");print $0}' ORS="" /tmp/players.list)
echo "PLAYERS5=$PLAYERS5" # debug trace

一旦您获得了所需的播放器列表。
确保以 JSON 格式正确喂食它们。

最后。

建议使用 JQ 命令准备有效的 JSON 单线字符串或来自 $ players 的对象。

| egrep -v "^LOG" | sed -e 's/^-/\\\\\n/') echo "PLAYERS=$PLAYERS" # debug trace set +x # disable debug trace

命令逐步:


建议尝试测试 players connected/


这应该打印/tmp/players.list list lines,直到找到Regexp Pattern /players connect/

滤除空线,并以 log 开头的行


应该打印/tmp/players.list所有行,直到找到Regexp Pattern /players connected/

log 开始过滤空线,

并以 - 替换为 \ code> \\ ,然后是新行。

例如:


将变换:


如果要将行与 \\ n contine contect:


例如:


will tr​​ansform:


可以简化/折叠 sed 命令和 egrep 命令命令到单个 awk 命令中。


一旦您获得了所需的播放器列表。
确保以 JSON 格式正确喂食它们。

最后。

建议使用 JQ 命令准备有效的 JSON 单线字符串或来自 $ players 的对象。

I understand the problem with PLAYERS variable generation.

Suggesting to debug with set -x and and set +x to inspect the variable:

set -x # enable debug trace
PLAYERS=$(cat /tmp/players.list | sed '/Players connected/q' | egrep -v '^

Suggesting to try test the PLAYERS command step by step:

PLAYERS1=$(sed '/Players connected/q' /tmp/players.list)
echo "PLAYERS1=$PLAYERS1" # debug trace

This should print /tmp/players.list all lines until found RegExp pattern /Players connected/

PLAYERS2=$(sed '/Players connected/q' /tmp/players.list| egrep -v '^([[:space:]]*$|LOG)')
echo "PLAYERS2=$PLAYERS2" # debug trace

This should print /tmp/players.list all lines until found RegExp pattern /Players connected/.

Filtering out empty lines, and Lines starting with LOG

PLAYERS3=$(sed '/Players connected/q' /tmp/players.list| egrep -v '^([[:space:]]*$|LOG)'| sed -e 's|^-|\\\\\n|')
echo "PLAYERS3=$PLAYERS3" # debug trace

This should print /tmp/players.list all lines until found RegExp pattern /Players connected/.

Filtering out empty lines, and Lines starting with LOG

And replacing lines starting with - to \\ followed by new line.

For example:

  line 1
  -line 2

Will transform:

  line 1
  \\
  line 2

If you want to concat the lines with \\n the command is:

PLAYERS4=$(sed '/Players connected/q' /tmp/players.list|egrep -v '^([[:space:]]*$|LOG)'| sed -z 's|\n-|\\\\n|')
echo "PLAYERS4=$PLAYERS4" # debug trace

For example:

  line 1
  -line 2

Will transform:

  line 1\\nline 2

It is possible to simplify/fold the sed commands and egrep command into single awk command.

PLAYERS5=$(awk '!/^([[:space:]]*$|LOG)/{sub("-","\\\\n");print $0}' ORS="" /tmp/players.list)
echo "PLAYERS5=$PLAYERS5" # debug trace

Once you have the desired PLAYERS list.
Make sure you feed them correctly in JSON format.

Finally.

Suggesting to use jq command to prepare a valid JSON single-line string or an object from $PLAYERS.

| egrep -v "^LOG" | sed -e 's/^-/\\\\\n/') echo "PLAYERS=$PLAYERS" # debug trace set +x # disable debug trace

Suggesting to try test the PLAYERS command step by step:


This should print /tmp/players.list all lines until found RegExp pattern /Players connected/


This should print /tmp/players.list all lines until found RegExp pattern /Players connected/.

Filtering out empty lines, and Lines starting with LOG


This should print /tmp/players.list all lines until found RegExp pattern /Players connected/.

Filtering out empty lines, and Lines starting with LOG

And replacing lines starting with - to \\ followed by new line.

For example:


Will transform:


If you want to concat the lines with \\n the command is:


For example:


Will transform:


It is possible to simplify/fold the sed commands and egrep command into single awk command.


Once you have the desired PLAYERS list.
Make sure you feed them correctly in JSON format.

Finally.

Suggesting to use jq command to prepare a valid JSON single-line string or an object from $PLAYERS.

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