If you have to write to a file that other process might read and that might result in a race condition (where writing to the file has not been completed yet), use mv, as it must be atomic according to POSIX.
If you have to create locking in a shell script, use directories, not files. mkdir is atomic, thus mkdir lockdir should never allow two processes to lock at the same time.
Don't use cat where it is not needed. Use grep string file instead of cat file | grep string. Also see Useless use of cat.