awk function problem

Posted on 2005-04-29
Last Modified: 2013-12-26
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)
awk 'BEGIN {
blankline = 1;
message = -1;
(/^From / && blankline) {
print "case 1"
blankline = 0;
if (message >= 0)
fname = sprintf("%04d", message);
print "(" fname ")" $0
if (NF == 0)
blankline = 1
blankline = 0;

### end
Question by:ALEx604
    LVL 20

    Accepted Solution

    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
    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?
    LVL 2

    Author Comment


    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.



    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Join & Write a Comment

    Suggested Solutions

    Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
    Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
    This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    25 Experts available now in Live!

    Get 1:1 Help Now