Web lists-archives.com

Re: bash usage.




peter@xxxxxxxxxxx writes:

> PROBLEM
> Given a storage device, /dev/GRN, and mount point, /home/peter/testdir/, 
> determine in a shell script whether /dev/GRN is mounted or something is 
> mounted on /home/peter/testdir/.
>
> ATTEMPTED SOLUTION
> Create testdir and execute this script.
>
> #!/bin/bash
> # Demonstrate use of "mountpoint" in a script to determine 
> # whether a directory has a device mounted on it.
> #
> WorkingDirectory=/home/peter/testdir
> if [ mountpoint $WorkingDirectory ]
>   then
>     echo A volume is mounted on $WorkingDirectory.
> else
>     echo Nothing mounted on $WorkingDirectory.
> fi
>
> This is my result.
> $ ./testscript
> ./testscript: line 6: [: mountpoint: unary operator expected
> Nothing mounted on /home/peter/testdir.
>
> According to 'man mountpoint', it returns 0 if something is mounted.  
> So why the complaint from 
>   if [ mountpoint $WorkingDirectory ] ?
>   

write it like this:

  if mountpoint $WorkingDirectory; then
      echo A Volume is mounted on $WorkingDirectory.
  else
      echo Nothing mounted on $WorkingDirectory.
  fi

The "[" is actually a command (usually implemented by the shell itself)
that has its own expression syntax.  See man test(1) for details.

-- 
regards,
kushal