As mentioned if [ $(tmux has -t junk) ]
expand to to if [ ]
which evaluates to false.
You could use:
if tmux has -t junk; then
echo OK
else
echo ERR
fi
Or if you want the shorter:
tmux has -t junk || echo ERR
Or
tmux has -t junk && echo OK || echo ERR
You can also negate it if that is more suitable, as in:
! tmux has -t junk || echo OK
! tmux has -t junk && echo ERR || echo OK
etc.
Edit:
Also not that if a command produces output you might want to redirect that
output to the black hole of /dev/null
as in:
if my_cmd >/dev/null; then echo OK else; echo ERR; fi
If the command produces textual error you'd might want to redirect standard
error as well by:
if my_cmd >/dev/null 2>&1; then echo OK else; echo ERR; fi
This, what follows, you might already know well, but add it for a bit more
completeness.
As mentioned: $?
is the only way to get the return value of the program.
Some programs vary return and can have a rather explicit meaning.
So e.g.:
mycmd
ecode=$?
case "$ecode" in
0) echo "Success";;
1) echo "Operation not permitted";;
2) echo "No such file or directory";;
esac
By this one can take appropriate action on specific errors.
If you have MySQL installed you can do e.g. this by perror:
for i in {0..50}; do perror $i; done
# And
for i in {1000..1050}; do perror $i; done
to get a feel for it.
See also this answer related to OS specific errors which also links to Open Groups doc Error Numbers and errno.h.
Or take a look at SQLite and its extended ones.
cmd
] expands to output of cmd. How if cmd work ? – bagavadhar Apr 12 '13 at 06:07cmd
has any output that would result in test being performed on that output.[
is a command with]
as last parameter. In turn that would most probably result in an error depending on output. Say thecmd
prints*Blah blah\n some info\n more blah*
all of that would be the test expression. You might want to look at the answers here. – Runium Apr 12 '13 at 06:30[
test command. – Runium Apr 12 '13 at 06:35