Skip to content
Advertisement

How logical operators works

I tried below but it doesn’t work. Throwing some errors. Exp1 or expr2 should evaluate first & at result should be evaluated with expr3 ( &&)

JavaScript

i tried as per suggestion, it throws error as

JavaScript

Fyi- that date is val1 value.

Advertisement

Answer

You might see different results based on the shell or version, eg: sh vs bash vs ash. One example is some bash yield an error from [ [ (with a separating space), while [[ (without a separating space) is accepted. Note there are different semantics within [ ] vs [[ ]] expressions.

Try dropping the outer space-separated [ and ] from your || expression. Also check documentation for operator precedence to meet or confirm your needs.

Another issue is the use of redirects < and >. Check your current directory for files named with the values from $val4 and $val5. Switch to -gt and -lt for comparisons.

Also to avoid surprises: when using -gt and -lt, check what happens when an operand is empty or non-numeric.

=======

May 24 update:

The original post did not specify a shell; now using ksh, there’s an issue with operator combos. Looks like you’ll need to use -o and -a with [ ] expressions or || and && with [[ ]] expressions. After switching to [[ ]] and replacing > and <, this version works on jdoodle.com :

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement