0

Is there any way to use || and && together in a single line command?

I'm looking to do something whose python equivalent would be:

import os
doSomething() if os.system("[[ -f hello.txt ]]") else doSomethingElse()
magikarp
  • 460
  • 1
  • 8
  • 22
  • 2
    The result is not identical. `a && b || c` **is not** the same as `if a; then b; else c; fi`. You get subtle bugs if you try to pretend that it is. – Charles Duffy Jun 11 '20 at 18:41
  • 1
    Specifically, you can have `c` run even if `a` is false, should `b` fail. And because `b` is marked "checked" by this idiom, `set -e`'s behavior is changed by it (not that using `set -e` is good practice). – Charles Duffy Jun 11 '20 at 18:43
  • 1
    (err, rather: even if `a` is true, should `b` fail). – Charles Duffy Jun 11 '20 at 18:56
  • Ah alright, got it! Btw, your answer at https://stackoverflow.com/questions/29351665/command-grouping is what I was looking for. Thank you! – magikarp Jun 11 '20 at 19:00

1 Answers1

0

There's no "AND" in your python example, is there?

In bash, I would write this simply as

if [ -f hello.txt ]; then
   doSomething
else
   doSomethingElse
fi
Gereon
  • 17,258
  • 4
  • 42
  • 73