1

I wrote this following piece of bash script.

if [ $(tmux has -t junk) ]
then
echo zero
else
echo one
fi

It always returns one, no matter if the session exists or not. I checked in the command line by executing the tmux statement and then running $? . It behaves as expected, zero if session exists and one if session does not exists Why isn't if statement is not behaving the same way. I tried changing the if statement like below

tmux has -t junk
if [ $? -eq 0 ]
then
echo zero
else
echo one
fi

The above thing works. So whats wrong with the first statement block ?

2 Answers 2

2

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.

3
  • Wow if tmux ... works! thanks. I understand if [ cmd ] expands to output of cmd. How if cmd work ?
    – bagavadhar
    Apr 12, 2013 at 6:07
  • @ashwin: Not sure I get what you ask. But if cmd 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 the cmd 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, 2013 at 6:30
  • @ashwin: Note e.g. comment on this answer on the [ test command.
    – Runium
    Apr 12, 2013 at 6:35
1

When you use:

if [ $(tmux has -t junk) ]

This checks the output of the command tmux has -t junk but not the return value.

So since it is always printing one as in the first case, this means that tmux has -t junk command is not printing anything on the standard output.

So in first case,

if [ $(tmux has -t junk) ] 

is getting evalauted as

if [ ]
2
  • How to check for return value ? Is $? the only way ?
    – bagavadhar
    Apr 12, 2013 at 4:50
  • Yes, $? is the only way to find out the return value. Apr 12, 2013 at 4:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.