英文:
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'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
"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>
# 答案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,我们需要用"'"
来包装它,以保持tsm_body.txt的格式和样式。
""'"${tsm_body}""'"
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
您可能需要阅读这个问题/答案
如果您坚持使用自己的方式,请尝试用"'"
包装您的$1
:
curl -s --data text=""'"$1"'"" ...
英文:
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="$(< $server_path/tsm_body.txt)"
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="$(< $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;
please notice the tsm_body which we have to wrap it with "'"
in order to preserver the tsm_body.txt format and style
"'"${tsm_body}"'"
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 '
/^#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
You may need reading this question/answer
If you insist your own way, please try wrapping your $1
in "'"
curl -s --data text="'"$1"'" ...
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论