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

Sylvain Leroux
  • 50,096
  • 7
  • 103
  • 125
user328721
  • 13
  • 3

1 Answers1

6

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."
vvvvv
  • 25,404
  • 19
  • 49
  • 81
Raul Andres
  • 3,766
  • 15
  • 24
  • 1
    Since it's unlikely you need the `echo $result` except for debugging, you can write this more simply as `if fuser /mount/foo; then` – chepner Jul 31 '14 at 15:11
  • chepner - your example would cause it to always echo "There are no processes accessing foo." echo $result is required because fuser /mount/foo doesn't provide the control I need unless the echo takes place. 1 reports the mount is in use and 0 reports it is not. Currently it always reports that it is in use because the first echo reports the mount being in use due to the fuser process. A second echo will provide a true answer, which is why this script is not complete. This is just a part of it. Now I need to add looping and other parameters. Thank you for your feedback. – user328721 Jul 31 '14 at 17:54