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.
||
and&&
look at the exit status of the latest executed command. If thegrep
in the assignment toa
returns a falsy status, the||
is fulfilled and theecho
runs. It in turn almost certainly returns a truthy status, which&&
looks for, so theexit
runs. On the other hand, if thegrep
returns a truthy status, theecho
is skipped, but theexit
runs. The combination of&&
and||
isn't the same asif-else
, which I think you probably should use here. – ilkkachu May 20 '21 at 04:42if
statement, I was just curious about this behaviour @ilkkachu – testoflow May 20 '21 at 04:55a=$(whatever) || { echo "error"; exit; } && echo success && some commands
. The braces would group theecho
andexit
, and in the failing case, the shell would exit before the&&
is checked. But justif a=$(whatever); then echo success; ...; else echo error; exit; fi
would still be clearer – ilkkachu May 20 '21 at 09:17