I want to conditionally submit a text into another shell script. Meaning I want to replace "WARNING" in case deb=1 with "INFO":
#!/usr/bin/env bash
...
if [[ $abc -lt 98 ]] || [[ $deb -eq 1 ]]
then
./discord.sh --webhook-url=$url --text "WARNING: $abc"
fi
I also want to avoid another complete IF statement. I expect to have something like
deb=1 ? "INFO" : "WARNING"
Does that work? If yes, how would the complete statement look like? "--text ..."
Thank you in advance.
CodePudding user response:
Can be done with an array index to match a numerical log-level with a name string:
#!/usr/bin/env bash
url=https://example.com/hook
logLevel=(WARNING INFO)
for abc in 97 98; do
for deb in 0 1; do
printf 'abc=%d, deb=%d:\n' $abc $deb
(((i = 1 == deb) || 98 > abc)) &&
echo ./discord.sh --webhook-url=$url --text "${logLevel[i]}: $abc"
done
done
Output:
abc=97, deb=0:
./discord.sh --webhook-url=https://example.com/hook --text WARNING: 97
abc=97, deb=1:
./discord.sh --webhook-url=https://example.com/hook --text INFO: 97
abc=98, deb=0:
abc=98, deb=1:
./discord.sh --webhook-url=https://example.com/hook --text INFO: 98
CodePudding user response:
You mean something like this?
[[ $abc -lt 98 ]] && {
./discord.sh --webhook-url=$url --text "$([[ $deb -eq 1 ]] && echo "INFO" || echo "WARNING"): $abc"
}
Think of this as a alternative to if
then
else
fi
(the curly brackets are only neccesary if you have more commands, but I like to use it anyway, for readability (in some cases).
Basically it's
[[ condition ]] && { commands if true } || { commands if false }
CodePudding user response:
Would you please try:
if (( abc < 98 || deb == 1 )); then
loglevel=$( (( deb == 1 )) && echo "INFO" || echo "WARNING" )
./discord.sh --webhook-url=$url --text "$loglevel: $abc"
fi
CodePudding user response:
IMHO the answer of @tshiono is best. However, it becomes hard to read/debug.
Why not use a verbose solution?
When both conditions are true, you want to log with INFO.
if ((deb==1 )); then
./discord.sh --webhook-url="$url" --text "INFO: $abc"
elif ((abc < 98)); then
./discord.sh --webhook-url="$url" --text "WARNING: $abc"
fi