awk function problem

I have a mbox parser from an old script that I have turned into a bash function,
but, as normal for me, there is what I believe to be a typo somewhere in it that I cannot find.
Or, I am missing a basic point in dealing with functions.

The error I get is:
"awk: cmd. line:22: (FILENAME=- FNR=1) fatal: expression for `>' redirection has null string value"
NOTE: because this function is within another script line 22 is print>fname; (I THINK) when run alone.

anyway command test is `mailsplit <mbox`


### BEGIN (#!/bin/bash gnu version)
mailsplit()
{
awk 'BEGIN {
blankline = 1;
message = -1;
}
(/^From / && blankline) {
print "case 1"
blankline = 0;
if (message >= 0)
{
close(fname);
}
message++;
fname = sprintf("%04d", message);
print "(" fname ")" $0
}
{
if (NF == 0)
blankline = 1
else
blankline = 0;

print>fname;
}
'
}
### end
LVL 2
ALEx604Asked:
Who is Participating?
 
tfewsterCommented:
The problem is that "fname" has not been defined the first time "print>fname" line is executed; This would happen if the first line of the awk input file doesn't start with "From"

You can avoid a problem with a malformed input file by e.g. defining
fname="junk"
in your BEGIN statement to catch any garbage lines

Also, I'm not clear how you're calling the function; Presumably there's a line  "mailsplit mbox" further down in the script, so that awk "knows" what its input file is?
0
 
ALEx604Author Commented:

Actually it turns out this function works fine.

The error I was getting was a result of something else in the script, totally my bad.

I thought I had deleted this question. Oh well.....

well, since it was my fault that this is still here, you get the points.

;)

ALEx604
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.