How to avoid a temp file

Hi all.

I want to check the free space on a given FS and process the output. Right now, I'm using a temp file to avoid using df twice. This is what I'm doing

#!/usr/bin/ksh
...
df -k $FS_NAME > $TMP_FILE 2>&1
if [[ $? -ne 0 ]]; then
RESULT="CRITICAL - $(cat $TMP_FILE)"
else
cat $TMP_FILE | ...
fi
rm $TMP_FILE

So, I store the result in a temp file, with the advantage of being able to store both sysout and syserr results I can later use as an output to the script. However, I don't like having to use a temp file.

So, is there some way to do the same without the temp file? Basically I want to:

  • Identify an error result from df (I'm using $? right now)
  • Get syserr output (I'm using 2>&1 now)
  • Process the resulting output.

If I just use a variable, as in A=$(...), I can't get the syserr, and I lose the carriage returns. Any ideas?

Thank you.

Hello.

I know this isn't really what you asked for, but it might be easier (less frustrating) to do something like that in Perl:

#!/usr/bin/env perl

my @output = `df -k / 2>&1`;
if ( $? != 0 ) {
        my $result = "CRITICAL - @output";
}else{
        print @output;
}

You could try storing the results in an array and then iterate through the results later. You'll lose any whitespace formatting, but it may help you get at what you need.

Here's a quick script I did in ksh on an AIX box:

#!/bin/ksh

# Store results of df -k in array
i=0
df -k | while read line
do
  tmp[$i]=$line
  i=$i+1
done

# Read through array
i=0
while [ $i -lt ${#tmp[@]} ]
do
  echo ${tmp[$i]}
  i=$i+1
done

As a matter of fact, I've used awk because I think it uses less resources than perl. I agree that it's easier in perl, but I'll use the script to monitorize several solaris servers, and wanted it to be as 'light' as possible. Does someone know what of the two approaches would be more efficient?