Home > OS >  How to store output in log file in this case:
How to store output in log file in this case:

Time:02-11

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

Above bash script successfully sends message to discord but at the same time I want it to store the same message in a log file. Without using webhook, earlier code was as follows:

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

Now I'm not sure how do I include >> file.log command in first script.

CodePudding user response:

Just merge the contents of the if block from the two scripts:

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

CodePudding user response:

If the content being sent to discord is just "success"

Shouldn't you just be able to do this?

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

Otherwise, if you are trying to store the response from curl, perhaps something like this could work?

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
  • Related