[bash] assign boolean expression to variable
I would like to assign true/false to $RUN in one line rather than using the clumsy if statement below.
Code:
: ${DELETE:=false} Code:
RUN=! $DELETE || $FORCE || $SKIP || [ -n "$RUNDAY" ] |
This should work:
Code:
! $DELETE || $FORCE || $SKIP || [ -n "$RUNDAY" ] && RUN=false |
Thank you for your reponse, jlliagre.
With the variables set as follows Code:
DELETE=false Code:
! $DELETE || $FORCE || $SKIP || [ -n "$RUNDAY" ] && RUN=false I would have expected bash to stop evaluating as soon as the any of the sub-expressions is found true. |
bash does not use the strings "true" and "false" to denote logical values true and false. What does it use? Here's from the "Advanced Bash-Scripting Guide 6.05"
Code:
#!/bin/bash |
Quote:
Code:
$DELETE && ! $FORCE && ! $SKIP && [ -z "$RUNDAY" ] && RUN=false Code:
! $DELETE || $FORCE || $SKIP || [ -n "$RUNDAY" ] || RUN=false |
ntubski, you cracked it!
However, I take catkin's point that I am not doing things the bash way. Coming from other programming languages, I find 0 = true totally counter-intuitive though. |
Quote:
That's our perspective as problem solvers; if it worked we don't care; if it failed we are interested! :) |
Quote:
:D |
Arithmetic Evaluation gives the more familiar 0=false C-like semantics:
Code:
DELETE=0 |
ntubski, I like that a lot, particularly the ${#var}!
However, the following now returns an error: Code:
$DELETE && rm -f $RUNFILE |
Because 1 and 0 only mean true and false inside arithmetic evaluation:
Code:
((DELETE)) && rm -f $RUNFILE |
All times are GMT -5. The time now is 06:54 AM. |