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

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

Run linux script after output printed with awk

问题

  1. 我正在处理一个脚本,在其中监视一些数据的读取和计算,在执行完这个脚本后,我想要打印输出并直接发送到一个 Telegram 频道,但为此我需要在 awk 输出之后执行一个命令。
  2. 消息已经发送到了 Telegram,但它们是不完整的(只有第一行,总共有 4 行每条消息)。
  3. 来自 AWK 的原始输出:
  4. `#Price_Notification_ProductABC
  5. Price: 0.007023
  6. change: +1.8%
  7. Count Number: 6`
  8. Telegram 上显示的输出:
  9. `#Price_Notification_ProductABC`
  10. 你能帮我解决问题吗?
  11. 谢谢
  12. 以下是我的脚本:
  13. awk.sh
  14. ```bash
  15. #!/bin/bash
  16. awk '
  17. /^#Price_Notification_Product/ {
  18. prod = $0;
  19. }
  20. /^change: / {
  21. gsub(/[+%]/,"",$2);
  22. products[prod] += $2;
  23. $2 = "+" products[prod] "%"
  24. }
  25. 1' input.txt | xargs -l ./telegram-send.sh

telegram-send.sh

  1. GROUP_ID=-myid
  2. BOT_TOKEN=mytoken
  3. # 这 3 个检查(if)并不是必需的,但应该是方便的
  4. if [ "$1" == "-h" ]; then
  5. echo "Usage: `basename $0` \"text message\""
  6. exit 0
  7. fi
  8. if [ -z "$1" ]
  9. then
  10. echo "Add message text as second arguments"
  11. exit 0
  12. fi
  13. if [ "$#" -ne 1 ]; then
  14. echo "You can pass only one argument. For string with spaces put it on quotes"
  15. exit 0
  16. fi
  17. curl -s --data "text=$1" --data "chat_id=$GROUP_ID" 'https://api.telegram.org/bot'$BOT_TOKEN'/sendMessage' > /dev/null
  1. <details>
  2. <summary>英文:</summary>
  3. 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.
  4. Messages are arriving on telegram, but they are arriving incomplete (only the first line, in total there are 4 lines per message)
  5. Original output from AWK:
  6. `#Price_Notification_ProductABC
  7. Price: 0.007023
  8. change: +1.8%
  9. Count Number: 6`
  10. Exit that is coming on telegram:
  11. `#Price_Notification_ProductABC`
  12. Could you help me to solve what is wrong?
  13. Thanks
  14. Here are my scripts:
  15. 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

  1. 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

  1. </details>
  2. # 答案1
  3. **得分**: 1
  4. ### telegram-send.sh
  5. 只需保存`awk`的输出并将其读取回您的**telegram-send.sh**文件。
  6. 以下是我从服务器发送通知到一个频道的方式:
  7. ```bash
  8. declare -r server_path=IF-YOU-HAVE-PATH
  9. declare -r shmlog_super_group=YOUR-GROUP
  10. declare -r shmdevbot=YOUR-BOT-TOKEN
  11. declare tsm_body
  12. # ...
  13. tsm_body="[$(< $server_path/tsm_body.txt)]"
  14. curl -sL \
  15. -o ${server_path}/
  16. </details>
  17. # 答案1
  18. **得分**: 1
  19. ### telegram-send.sh
  20. 只需保存`awk`的输出并将其读取回您的**telegram-send.sh**文件。
  21. 以下是我从服务器发送通知到一个频道的方式:
  22. ```bash
  23. declare -r server_path=IF-YOU-HAVE-PATH
  24. declare -r shmlog_super_group=YOUR-GROUP
  25. declare -r shmdevbot=YOUR-BOT-TOKEN
  26. declare tsm_body
  27. # ...
  28. tsm_body="[$(< $server_path/tsm_body.txt)]"
  29. curl -sL  \
  30. -o ${server_path}/${0}.log \
  31. -X POST \
  32. -H 'Content-Type: application/json' \
  33. -d '{ "parse_mode": "HTML", "chat_id": "'"${shmlog_super_group}"'", "text": "'"${tsm_body}"'", "disable_notification": "false" }' \
  34. https://api.telegram.org/bot$shmdevbot/sendMessage;
  35. .log \
  36. -X POST \
  37. -H 'Content-Type: application/json' \
  38. -d '{ "parse_mode": "HTML", "chat_id": "'"${shmlog_super_group}"'", "text": "'"${tsm_body}"'", "disable_notification": "false" }' \
  39. https://api.telegram.org/bot$shmdevbot/sendMessage;

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

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

awk.sh

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

例如:

  1. #!/bin/bash
  2. awk '
  3. /^#Price_Notification_Product/ {
  4. prod = $0;
  5. }
  6. /^change: / {
  7. gsub(/[+%]/,"",$2);
  8. products[prod] += $2;
  9. $2 = "+" products[prod] "%";
  10. }
  11. 1' > input.txt
  12. echo /path/to/telegram-send.sh /path/to/input.txt | at now +1 minute

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

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

  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

  1. declare -r server_path=IF-YOU-HAVE-PATH
  2. declare -r shmlog_super_group=YOUR-GROUP
  3. declare -r shmdevbot=YOUR-BOT-TOKEN
  4. declare tsm_body
  5. ...
  6. ...
  7. ...
  8. tsm_body=&quot;$(&lt; $server_path/tsm_body.txt)&quot;
  9. curl -sL \
  10. -o ${server_path}/
    declare -r server_path=IF-YOU-HAVE-PATH
  11. declare -r shmlog_super_group=YOUR-GROUP
  12. declare -r shmdevbot=YOUR-BOT-TOKEN
  13. declare tsm_body
  14. ...
  15. ...
  16. ...
  17. tsm_body=&quot;$(&lt; $server_path/tsm_body.txt)&quot;
  18. curl -sL  \
  19. -o ${server_path}/${0}.log \
  20. -X POST \
  21. -H &#39;Content-Type: application/json&#39; \
  22. -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; \
  23. https://api.telegram.org/bot$shmdevbot/sendMessage;
  24. .log \
  25. -X POST \
  26. -H &#39;Content-Type: application/json&#39; \
  27. -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; \
  28. 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

  1. &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:

  1. #!/bin/bash
  2. awk &#39;
  3. /^#Price_Notification_Product/ {
  4. prod = $0;
  5. }
  6. /^change: / {
  7. gsub(/[+%]/,&quot;&quot;,$2);
  8. products[prod] += $2;
  9. $2 = &quot;+&quot; products[prod] &quot;%&quot;
  10. }
  11. 1&#39; &gt; input.txt
  12. 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;

  1. 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:

确定