what does this piece of code do?

Hi All,

I am trying to understand and change some code written by some programmer a while ago. There are following three lines of code that I am unable to grasp. Could anybody please help me understand it?

1) cd - > /dev/null

2) fname=`basename "$1"` where $1 = /dirA/dirB/a.txt
dname="${1%/$fname}"

I understand in this case fname would give me a.txt but what will dname be? I tried to echo dname on command line but it comes out to be blank

3) var1=/xx/yy
var2=${var1##*/}1

The result of the above is yy1. How does var2 get set to yy1?

4) inUse=`fuser "$1" 2>&1`
outputLines=`cat <<! | wc -l
${inUse}
!`

What I dont understand in above code is, if the intent of the programmer is just to count the number of rows returned by inUse variable, why did she choose to have <<! ?? Is there a good reason behind it or is just too complex without any good reason?

Thanks guyz.

1) Change to the previous directory and redirect any output to the bit bucket.
2) dname takes the fully qualified filename and removes "/a.txt" leaving only the path.

e.g. "/dirA/dirB"

3) By removing all characters to the left of the last "/" and including the "/" and appending "1" to it. This uses the shell built-in pattern matching capabilities and effectively strips off all text to and including the last "/" character.

Hi
Thanks for your reply. I had edited the post to include a fouth line of code? Could you plelase help me understand that as well?
TIA
Vikas.

4) No doubt that the back ticks would have been messed up in the here document and this is the easiest implementation for her. inUse is running the fuser command and returning its output into the inUse variable. Then a count is determined by using cat on a here document on the previous command's output. This is done no doubt to accomplish the tasks without creating temporary files.