运行Linux脚本,在使用awk打印输出后。

huangapple go评论93阅读模式
英文:

Run linux script after output printed with awk

问题

我正在处理一个脚本,在其中监视一些数据的读取和计算,在执行完这个脚本后,我想要打印输出并直接发送到一个 Telegram 频道,但为此我需要在 awk 输出之后执行一个命令。

消息已经发送到了 Telegram,但它们是不完整的(只有第一行,总共有 4 行每条消息)。

来自 AWK 的原始输出:

`#Price_Notification_ProductABC
Price: 0.007023
change: +1.8%
Count Number: 6`

在 Telegram 上显示的输出:

`#Price_Notification_ProductABC`

你能帮我解决问题吗?
谢谢

以下是我的脚本:

awk.sh
```bash
#!/bin/bash

awk '
/^#Price_Notification_Product/ {
  prod = $0;
}
/^change: / {
  gsub(/[+%]/,"",$2);
  products[prod] += $2;
  $2 = "+" products[prod] "%"
}
1' input.txt | xargs -l ./telegram-send.sh

telegram-send.sh

    
GROUP_ID=-myid
BOT_TOKEN=mytoken

# 这 3 个检查(if)并不是必需的,但应该是方便的
if [ "$1" == "-h" ]; then
  echo "Usage: `basename $0` \"text message\""
  exit 0
fi

if [ -z "$1" ]
  then
    echo "Add message text as second arguments"
    exit 0
fi

if [ "$#" -ne 1 ]; then
    echo "You can pass only one argument. For string with spaces put it on quotes"
    exit 0
fi

curl -s --data "text=$1" --data "chat_id=$GROUP_ID" 'https://api.telegram.org/bot'$BOT_TOKEN'/sendMessage' > /dev/null


<details>
<summary>英文:</summary>

I&#39;m working on a script where I monitor the reading and calculation of some data, after executing this script I would like to print the output and send it directly to a telegram channel, but for that I need to execute a command after the awk output.

Messages are arriving on telegram, but they are arriving incomplete (only the first line, in total there are 4 lines per message)

Original output from AWK:

`#Price_Notification_ProductABC
Price: 0.007023
change: +1.8%
Count Number: 6`

Exit that is coming on telegram:

`#Price_Notification_ProductABC`

Could you help me to solve what is wrong?
Thanks

Here are my scripts:

awk.sh

#!/bin/bash

awk '
/^#Price_Notification_Product/ {
prod = $0;
}
/^change: / {
gsub(/[+%]/,"",$2);
products[prod] += $2;
$2 = "+" products[prod] "%"
}
1' input.txt | xargs -l ./telegram-send.sh


telegram-send.sh

#!/bin/bash

GROUP_ID=-myid
BOT_TOKEN=mytoken

this 3 checks (if) are not necessary but should be convenient

if [ "$1" == "-h" ]; then
echo "Usage: basename $0 &quot;text message&quot;"
exit 0
fi

if [ -z "$1" ]
then
echo "Add message text as second arguments"
exit 0
fi

if [ "$#" -ne 1 ]; then
echo "You can pass only one argument. For string with spaces put it on quotes"
exit 0
fi

curl -s --data "text=$1" --data "chat_id=$GROUP_ID" 'https://api.telegram.org/bot'$BOT_TOKEN'/sendMessage' > /dev/null






</details>


# 答案1
**得分**: 1

### telegram-send.sh
只需保存`awk`的输出并将其读取回您的**telegram-send.sh**文件。

以下是我从服务器发送通知到一个频道的方式:

```bash
declare -r server_path=IF-YOU-HAVE-PATH
declare -r shmlog_super_group=YOUR-GROUP
declare -r shmdevbot=YOUR-BOT-TOKEN
declare tsm_body

# ...

tsm_body="[$(< $server_path/tsm_body.txt)]"

curl -sL  \
    -o ${server_path}/

</details>
# 答案1
**得分**: 1
### telegram-send.sh
只需保存`awk`的输出并将其读取回您的**telegram-send.sh**文件。
以下是我从服务器发送通知到一个频道的方式:
```bash
declare -r server_path=IF-YOU-HAVE-PATH
declare -r shmlog_super_group=YOUR-GROUP
declare -r shmdevbot=YOUR-BOT-TOKEN
declare tsm_body
# ...
tsm_body="[$(< $server_path/tsm_body.txt)]"
curl -sL  \
-o ${server_path}/${0}.log \
-X POST \
-H 'Content-Type: application/json' \
-d '{ "parse_mode": "HTML", "chat_id": "'"${shmlog_super_group}"'", "text": "'"${tsm_body}"'", "disable_notification": "false" }' \
https://api.telegram.org/bot$shmdevbot/sendMessage;
.log \ -X POST \ -H 'Content-Type: application/json' \ -d '{ "parse_mode": "HTML", "chat_id": "'"${shmlog_super_group}"'", "text": "'"${tsm_body}"'", "disable_notification": "false" }' \ https://api.telegram.org/bot$shmdevbot/sendMessage;

请注意tsm_body,我们需要用&quot;&#39;&quot;来包装它,以保持tsm_body.txt的格式和样式。

"&quot;&#39;&quot;${tsm_body}"&quot;&#39;&quot;

awk.sh

由于我们应该从文件中读取awk.sh的输出,因此不能再使用xargs。只需使用at使您的脚本不再依赖于awk.sh

例如:

#!/bin/bash

awk '
/^#Price_Notification_Product/ {
  prod = $0;
}
/^change: / {
  gsub(/[+%]/,"",$2);
  products[prod] += $2;
  $2 = "+" products[prod] "%";
}
1' > input.txt

echo /path/to/telegram-send.sh /path/to/input.txt | at now +1 minute

您可能需要阅读这个问题/答案

如果您坚持使用自己的方式,请尝试用&quot;&#39;&quot;包装您的$1

curl -s --data text="&quot;&#39;&quot;$1&quot;&#39;&quot;" ...
英文:

telegram-send.sh

Simply you can save the output of awk and read it back to your telegram-send.sh file.

here is the way I send notification from a server to a channel

declare -r server_path=IF-YOU-HAVE-PATH
declare -r shmlog_super_group=YOUR-GROUP
declare -r shmdevbot=YOUR-BOT-TOKEN
declare tsm_body

...
...
...

tsm_body=&quot;$(&lt; $server_path/tsm_body.txt)&quot;

curl -sL  \
    -o ${server_path}/
declare -r server_path=IF-YOU-HAVE-PATH
declare -r shmlog_super_group=YOUR-GROUP
declare -r shmdevbot=YOUR-BOT-TOKEN
declare tsm_body
...
...
...
tsm_body=&quot;$(&lt; $server_path/tsm_body.txt)&quot;
curl -sL  \
-o ${server_path}/${0}.log \
-X POST \
-H &#39;Content-Type: application/json&#39; \
-d &#39;{ &quot;parse_mode&quot;: &quot;HTML&quot;, &quot;chat_id&quot;: &quot;&#39;&quot;$shmlog_super_group&quot;&#39;&quot;, &quot;text&quot;: &quot;&#39;&quot;${tsm_body}&quot;&#39;&quot;, &quot;disable_notification&quot;: &quot;false&quot; }&#39; \
https://api.telegram.org/bot$shmdevbot/sendMessage;
.log \ -X POST \ -H &#39;Content-Type: application/json&#39; \ -d &#39;{ &quot;parse_mode&quot;: &quot;HTML&quot;, &quot;chat_id&quot;: &quot;&#39;&quot;$shmlog_super_group&quot;&#39;&quot;, &quot;text&quot;: &quot;&#39;&quot;${tsm_body}&quot;&#39;&quot;, &quot;disable_notification&quot;: &quot;false&quot; }&#39; \ https://api.telegram.org/bot$shmdevbot/sendMessage;

please notice the tsm_body which we have to wrap it with &quot;&#39;&quot; in order to preserver the tsm_body.txt format and style

&quot;&#39;&quot;${tsm_body}&quot;&#39;&quot;

awk.sh

Since we should read the output of awk.sh from a file, no longer you can use xargs. Simply use at to make your script independent of awk.sh

for example:

#!/bin/bash

awk &#39;
/^#Price_Notification_Product/ {
  prod = $0;
}
/^change: / {
  gsub(/[+%]/,&quot;&quot;,$2);
  products[prod] += $2;
  $2 = &quot;+&quot; products[prod] &quot;%&quot;
}
1&#39; &gt; input.txt

echo /path/to/telegram-send.sh /path/to/input.txt | at now +1 minute

You may need reading this question/answer


If you insist your own way, please try wrapping your $1 in &quot;&#39;&quot;

curl -s --data text=&quot;&#39;&quot;$1&quot;&#39;&quot; ...

huangapple
  • 本文由 发表于 2023年7月10日 12:51:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/76650742.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定