For semaphores, read the the following link....hope it helps u:
http://www.postgresql.org/docs/7.4/static/kernel-resources.html
For memory:
u can use the command:
root# free
Ok, some tips writing the script....
1. Use awk for handling field separators
2. Write a good commented script
3. Write a good logger to re-use all the time
4. Learn to use the debugger mode for shell scripts, when u face notorius and hard to find errors ***WARNING ---> This is very resource expensive***
For tip#4 go here --->
http://www.tldp.org/LDP/Bash-Beginners-Guide/html/sect_02_03.html