Question: I need to find if a location is a directory and if it is a mount in ShellScript

I tried this:

```
new_dir="/home/fruits/apple"
if [ -d "$new_dir" ]
then
    if [ mountpoint -q "$new_dir" ]
    then
        echo "directory is mounted"
    else
        echo "directory is not mounted"
    fi
fi

```

But it is not working.
gives error like

```
test_dir.sh: line 13: [: -q: binary operator expected
directory is not mounted

```

Can someone help... I need this working...

Answer: 
The key is in saying:

```
if [ mountpoint -q "$new_dir" ]   # WRONG

```

You don't have to use [test](http://ss64.com/bash/test.html) `[` because `mountpoint -q "$new_dir"` already returns a boolean. So this is all you need:

```
if mountpoint -q "$new_dir"

```

From `man point`:

> 
> Mountpoint checks if the directory is a mountpoint.
> 
> 
> EXIT STATUS
> 
> 
> Zero if the directory is a mountpoint, non-zero if not.
> 
> 
> 

In fact you can even say:

```
mountpoint -q "$new_dir" && echo "mounted" || echo "not mounted"

```

If you really want to use `[ ]`, then you need to execute the command inside and take into consideration the exit status:

```
if [ ! $(mountpoint -q "/home") ]; then
    echo "directory is mounted"
else
    echo "directory is not mounted"
fi

```

Note I negate the result with `[ ! expression ]`.

---

### Why were you getting a *binary operator expected* error?

Because you were writing an expression in test that is not supported.

`man test` explains the syntax it accepts:

```
[ EXPRESSION ]
[ ]
[ OPTION

```

And it also mentions that it exits *with the status determined by EXPRESSION*.

When you say `mountpoint` within `[`, bash assumes that it is handling an expression. Given the list of possible conditions ([6.4 Bash Conditional Expressions](http://www.gnu.org/software/bash/manual/html_node/Bash-Conditional-Expressions.html)), the fact that the first element is not an operator makes bash want to see a binary operator after it, such as `=`, `=~`... Since it sees `-q`, it fails and mentions `-q` as the problem.
@fedorqui is right.

The conditional construct:

```
if [ mountpoint -q "$new_dir" ]

```

is interpreted as "mountpoint" <operator> <variable> inside `[...]`.

So `-q` is seen as the operator, and there is no such operator taking two arguments.