How to avoid a temp file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to avoid a temp file
# 1  
Old 10-24-2008
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.
# 2  
Old 10-24-2008
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:

Code:
#!/usr/bin/env perl

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


Last edited by vimes; 10-24-2008 at 09:12 AM.. Reason: Code fix
# 3  
Old 10-24-2008
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:

Code:
#!/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

# 4  
Old 10-24-2008
Quote:
Originally Posted by vimes
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:
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?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to avoid the "temp files" in my script?

Hi ! :) I noticed that I create often of temporary files to keep character strings or other and I wish to avoid that if possible ? e.g : #!/bin/bash CONFIG_FILE="conf.cfg" TEMP_HOSTNAME="temp_file1.txt" for IP in `egrep -o '({1,3}\.){3}{1,3}' $CONFIG_FILE` do ssh "$IP"... (2 Replies)
Discussion started by: Arnaudh78
2 Replies

2. Shell Programming and Scripting

Store the name of an extracted file to a temp file

What would be the best way to store the name of an extracted file from a tar to a text file? I want to extract one file from a tar and store the name of the extracted file to a temp file. tar -xvf tar_file.tar file_to_be_extracted (1 Reply)
Discussion started by: erin00
1 Replies

3. Shell Programming and Scripting

Lookup on large file based on a temp file

hello guys Please help me with the below issue I have two files one base file another lookupfile base file abc-001 bcd-001 cde-001 Lookupfile abc-001|11|12 abc-001|11|12 abc-001|11|12 (6 Replies)
Discussion started by: Pratik4891
6 Replies

4. Shell Programming and Scripting

I/O Redirection: how can I redirect error msgs to a temp file

Hi there, I just want to know how can I redirect error msgs returned from running a command (e.g. nslookup) and then only print out the correct output using an if statement?. I've tried the following: where $a is a list of IPs. but I got all the error msgs printed out to screen and... (9 Replies)
Discussion started by: Abdulelah
9 Replies

5. Shell Programming and Scripting

Joining 3 AWK scripts to avoid use "temp" files

Hi everyone, Looking for a suggestion to improve the below script in which I´ve been working. The thing is I have 3 separated AWK scripts that I need to apply over the inputfile, and for scripts (2) and (3) I have to use a "temp" file as their inputfile (inputfile_temp and inputfile_temp1... (2 Replies)
Discussion started by: cgkmal
2 Replies

6. Shell Programming and Scripting

find and replace a string in a file without the use of temp file

Hi - I am looking for a replacing a string in a in multiple *.sql files in directory with a new string without using a temporary file Normally I can use sed command as below for W in ls `FILE*.sql` do sed 's/OLD/NEW/g' $W > TEMPFILE.dat mv TEMPFILE.dat $W done But Here in my... (9 Replies)
Discussion started by: raghutapal
9 Replies

7. UNIX for Dummies Questions & Answers

Updating a field in a File without creating temp file's

Hi Experts, I have a requirement where i need to update the below items in file, 1. END TIME 2. PREV_STATUS For the first time the PREV_status and end time of all job the job will be sysdate & NULL reply as below, Session_name,Load Type,Frequency,Seesion End time,Prev_Status... (2 Replies)
Discussion started by: prabhutkl
2 Replies

8. UNIX and Linux Applications

mail: no space for temp file

I'm trying to read mail but I obtained this message: msgcnt 2446 vxfs: mesg 001: vx_nospace - /dev/vg00/lvol4 file system full (1 block extent) mail: no space for temp file I want to empty my mailbox How could I make this? Regards Leonardo Siza (3 Replies)
Discussion started by: usernamea
3 Replies

9. Shell Programming and Scripting

Script to Delete temp files and check file system

Hi all, new to the threads as well as Unix/Linux. I need to create a script that will delete any temporary files as well as check the files on the system for errors or corruption. This is what I have so far and I'm sure that I'm missing things or have the wrong commands. I'm not sure where to go... (3 Replies)
Discussion started by: Bwood1377
3 Replies

10. Shell Programming and Scripting

Append to end of each line of file without a temp file.

Hello I am trying to append an incrimenting number to the end of each line I have it working with a temp file. But I want to do this without a temp file. a=1 cat "file" | while read LINE do echo "$LINE, $a" >> filewithnumbers a=`expr $a + 1` ... (4 Replies)
Discussion started by: rorey_breaker
4 Replies
Login or Register to Ask a Question