First things first:
if [ "$temp" == " " ]
The comparison operator is actually =, not ==. The latter works in some shells, but isn't standard. Also, you're comparing the variable not against an empty string, but one containing a single space, which may or may not be what your file has.
If the file can contain extra whitespace, it might be a good idea to just remove all of it with sed or tr. And then you can just use [ -z "$temp" ] to test if it's empty. So:
temp=$(head -5 $filename | tail -1 | tr -d ' \t\r') if [ -z "$temp" ] ; then ...
Instead of head and tail, you can use sed to pick a single line:
temp=$(sed -n '5p' | tr -d ' \t\r') if [ -z "$temp" ] ; then ...
(5p means "on line 5, (p)rint the line", and -n inhibits the default action of printing any lines.)
Actually, instead of the test I'm a bit tempted to just go through the file with an awk script to get the fifth or sixth line depending on if the fifth has anything:
temp=$(tr -d ' \t\r' < $filename1 | awk '(NR==5 && $0) || NR==6 {print; exit}' )
(NR==5 tests the current line (record) number, $0 expands to the contents of the line, and is "true" if the line is not empty, && and || are the usual and and or operations)
temp=$(head -5 $filename | tail -1 | tr -d '\r\n')