user328721
user328721

Reputation: 13

fuser bash script returning unanticipated output

#!/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.

Upvotes: 1

Views: 1402

Answers (1)

Raul Andres
Raul Andres

Reputation: 3806

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."

Upvotes: 6

Related Questions