fuser bash script returning unanticipated output

1.3k views Asked by At
#!/bin/bash

fuser /mount/foo
echo $?

if [ $? = 0 ]; then
    echo "There are no processes accessing foo."
else
    echo "foo is in use."

Echo$? is returning '1' because the fuser process is accessing the mount - rather than echoing "The mount is in use," it echoes "There are no processes accessing the mount." I'm not sure what could be causing this contrary behavior aside from syntax but maybe I'm building it completely incorrectly.

1

There are 1 answers

2
Raul Andres On BEST ANSWER

Your second $? evaluates the result of echo, that is supposed to be 0. Remove echo or use a variable instead:

#!/bin/bash

fuser /mount/foo
result=$?
echo $result

if [ $result = 0 ]; then
    echo "There are no processes accessing foo."
else
    echo "foo is in use."