Question: My bash code look likes:

```
  set -e
  (
     flock -n 9
     main $@
   ) 9>/var/lock/mylockfile

```

But currently it don't show any error message, if it is not able to get lock on the file. 
Is there a way to show error message ?

Answer: 
With "set -e", you can use the ERR signal. As said in the man page of bash
(description of the "set" command with option "-e"): 

```
"A trap on ERR, if set, is executed before the shell exits."

```

So you can try something like:

```
set -e
(
  trap 'echo flock failed.' ERR
  flock -n 9
  trap - ERR   # reset ERR trap
  main $@
) 9>/var/lock/mylockfile

```
The example in the manpage says:

```
(
     flock -n 9 || exit 1
     # ... commands executed under lock ...
   ) 9>/var/lock/mylockfile

```

ie it exits if flock fails -- why don't you just use that?
If you want to display an error, you could try this: 

```
(
         # paranoia: flock may fail with an exit code other than 1,
         # eg if it can't be found in $PATH
         if flock -n 9 ; then
           do_stuff
         else
           show_error
         fi
) 9>$lockfile

```