shell 在用awk打印输出后运行linux脚本

ovfsdjhp  于 2023-08-07  发布在  Shell
关注(0)|答案(1)|浏览(102)

我正在编写一个脚本,在此脚本中我监视一些数据的阅读和计算,在执行此脚本后,我想打印输出并将其直接发送到电报通道,但为此,我需要在awk输出后执行命令。
报文通过电报到达,但到达时不完整(只有第一行,每条报文总共有4行)
AWK的原始输出:
第一个月
电报上即将出现的出口:
#Price_Notification_ProductABC
你能帮我解决问题吗?谢啦,谢啦
以下是我的脚本:
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

6uxekuva

6uxekuva1#

telegram-send.sh

您可以简单地保存awk的输出并将其读回您的telegram-send.sh文件。
下面是从服务器向通道发送通知的方式

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;

字符串
请注意tsm_body,我们必须用"'" Package 它,以便保留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


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

  • 使用curl POST和bash脚本函数中定义的变量

如果您坚持自己的方式,请尝试将$1 Package 在"'"

curl -s --data text="'"$1"'" ...

相关问题