在这种情况下如何将输出存储在日志文件中:

How to store output in log file in this case:

webhook=https://webhook-url

if [[ some_condition ]]; then
curl "some_command..." -d '{"content":"'"success"'"}' $webhook #sends message to discord if condition is met
else
curl "some_command..." -d '{"content":"'"error"'"}' $webhook #sends message to discord if condition isn't met
fi

上面的 bash 脚本成功地向 discord 发送了消息,但同时我希望它将相同的消息存储在日志文件中。不使用webhook,之前的代码如下:

if [[ some_condition ]]; then
echo "success" >> file.log
else
echo "error" >> file.log
fi

现在我不确定如何在第一个脚本中包含 >> file.log 命令。

只需合并来自两个脚本的 if 块的内容:

if [[ some_condition ]]; then
    message="Some 'success' information."
    curl "some_command..." -d '{"content":"$message"}' $webhook #sends message to discord if condition is met
    echo $message >> file.log
else
    message="Some 'error' information."
    curl "some_command..." -d '{"content":"$message"}' $webhook #sends message to discord if condition isn't met
    echo $message >> file.log
fi

如果发送到 discord 的内容只是“成功”

你不应该只做这个吗?

webhook=https://webhook-url

if [[ some_condition ]]; then
curl "some_command..." -d '{"content":"'"success"'"}' $webhook
echo "success" >> file.log
else
curl "some_command..." -d '{"content":"'"error"'"}' $webhook 
echo "error" >> file.log
fi

否则,如果您尝试存储来自 curl 的响应,也许这样的方法可行?

webhook=https://webhook-url

if [[ some_condition ]]; then
res=$(curl "some_command..." -d '{"content":"'"success"'"}' $webhook)
echo "$res" >> file.log
else
res=$(curl "some_command..." -d '{"content":"'"error"'"}' $webhook)
echo "$res" >> file.log
fi

更新:

也许试试这个?

cmd="curl "some_command..." -d '{"content":"'"success"'"}' $webhook"
echo $cmd >> file.log