In basic 'shell', how do I put the result of a string comparison into a boolean variable?
Consider:
isweekday() {
w=$(date %w)
if [[ $w == "0" ]] || [[ $w == "6" ]]; then
false
else
true
fi
}
One can debate whether it would be clearer or not, but is there a way to assign the result of the 'if' expression to a boolean variable, e.g.
isweekday() {
w=$(date %w)
wd=<boolean result of [[ $w == "0" ]] || [[ $w == "6" ]]>
return $wd
}
After some experimentation, I got closer to what I want but it still isn't what I'm after:
isweekday() {
w=$(date %w)
[[ $w == "0" ]] || [[ $w == "6" ]]
}
This works and does not require the conditional, but it looks wrong, however if you do:
if isweekday; then
echo 'weekday'
fi
you will get the right result. This seems to be because the exit code of 'true' is 0 and the exit code of 'false' is 1 (not quite sure why that is...)
CodePudding user response:
There are no boolean variables. All shell variables are strings (though there are limited facilities for interpreting them as integer numbers for basic comparisons etc and basic arithmetic).
The exit code 0 is reserved for success; all other exit codes (up to the maximum 255) signify an error (though some nonstandard tools use this facility to communicate non-error results by way of convention; the caller is then assumed to understand and agree on this ad-hoc use of the exit code).
Perhaps then the simplest answer to what you appear to be asking is to save the exit code. It is exposed in the variable $?
. But very often, you should not need to explicitly examine its value; indeed, your final isweekday
code looks by far the most idiomatic and natural, and returns this code from [[
implicitly.
I don't understand why you think your final code "looks wrong"; this is precisely how you would use a function in a conditional.
CodePudding user response:
The standard approach in Shell is to use the exit code of a command as the true/false value where zero indicates true and non-zero false. Also, I would instead define a function which test if it's weekend today since that is the exception so to speak. When not using a conditional I prefer to use the test command instead of brackets. Finally, it's a good idea to define w as a local variable and to quote all variables which could in the general case contain a space or be undefined; in the latter case the quoting prevents a syntax error.
IsWeekend()
{
local w="$(date %w)"
test "$w" = 0 -a "$w" = 6
}