Search code examples
bashfuser

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.


Solution

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