0

I don't know how to title this question but anyway

a="$(file . * | grep -i pgp)" || echo "no such files found! " && exit \
        && b=$(echo "$a" | fzf --cycle --height=15) && [ -n "$b" ] \
        && gpg --import "$b"

I'm confused about this. When variable a does not exist prints echo "no such files found! " and exits but when does exist doesn't print the message and exits as well. How can I do to exit only when no value is passed to variable a, in other words, when variable a does not exist.

testoflow
  • 117
  • 8
  • 1
    `||` and `&&` look at the exit status of the latest executed command. If the `grep` in the assignment to `a` returns a falsy status, the `||` is fulfilled and the `echo` runs. It in turn almost certainly returns a truthy status, which `&&` looks for, so the `exit` runs. On the other hand, if the `grep` returns a truthy status, the `echo` is skipped, but the `exit` runs. The combination of `&&` and `||` isn't the same as `if-else`, which I think you probably should use here. – ilkkachu May 20 '21 at 04:42
  • yeah, normally I use operators but I ended up with the `if` statement, I was just curious about this behaviour @ilkkachu – testoflow May 20 '21 at 04:55
  • 1
    I guess you could do something like `a=$(whatever) || { echo "error"; exit; } && echo success && some commands`. The braces would group the `echo` and `exit`, and in the failing case, the shell would exit before the `&&` is checked. But just `if a=$(whatever); then echo success; ...; else echo error; exit; fi` would still be clearer – ilkkachu May 20 '21 at 09:17

0 Answers0