Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Capture output of command and save to text file 1

Status
Not open for further replies.

n3tw0rkadm1n1strat0r

IS-IT--Management
Aug 29, 2006
119
US
I am new to perl and have started this script going off of another one. I am trying to capture the output of a command and save it to a text file. Here is what I have so far:

Code:
#!C:\Perl\bin\perl.exe

open (IN, "omnidb -session -latest -detail|") ;
while (<IN>)
{
$data = $_ ;


 
Code:
open (IN, "omnidb -session -latest -detail|") or die "$!";
open(OUT, ">>output.txt") or die "$!":
while (<IN>)
{
print OUT $_ ;
}

- Kevin, perl coder unexceptional!
 
Kevin's in a hurry today - a small typo:
Code:
open(OUT, ">>output.txt") or die "$!"[red][b];[/b][/red]

You could also use backticks:
Code:
open(OUT, ">>output.txt") or die "$!";
print OUT `omnidb -session -latest -detail`;
 
Thanks...how would I make it so the file overwrites? I want to send an email too with the file attached.
 
Ahh ok just looked it up to overwrite, only one ">"

open(OUT, ">output.txt") or die "$!";

But emailing the file looks tricky.
 
Here's something I dug up from the Internet real quick in regards to e-mailing attached files:

Code:
#!/usr/bin/perl

# Akadia AG, Arvenweg 4, CH-3604 Thun                 send_attachment.pl
# ----------------------------------------------------------------------
#
# File:       send_attachment.pl
#
# Autor:      Martin Zahn / 05.01.2003
#
# Purpose:    Email attachments in Perl
#
# Location:   $ORACLE_HOME\Database
#
# Certified:  Perl 5.6.1, MIME-Lite-2.117 on Cygwin / Windows 2000
# ----------------------------------------------------------------------

use MIME::Lite;
use Net::SMTP;

### Adjust sender, recipient and your SMTP mailhost
my $from_address = 'martin.zahn@akadia.com';
my $to_address = 'martin.zahn@akadia.com';
my $mail_host = 'mailhost.domain.com';

### Adjust subject and body message
my $subject = 'A message with 2 parts ...';
my $message_body = "Here's the attachment file(s) you wanted";

### Adjust the filenames
my $my_file_gif = 'my_file.gif';
my $your_file_gif = 'your_file.gif';
my $my_file_zip = 'my_file.zip';
my $your_file_zip = 'your_file.zip';

### Create the multipart container
$msg = MIME::Lite->new (
  From => $from_address,
  To => $to_address,
  Subject => $subject,
  Type =>'multipart/mixed'
) or die "Error creating multipart container: $!\n";

### Add the text message part
$msg->attach (
  Type => 'TEXT',
  Data => $message_body
) or die "Error adding the text message part: $!\n";

### Add the GIF file
$msg->attach (
   Type => 'image/gif',
   Path => $my_file_gif,
   Filename => $your_file_gif,
   Disposition => 'attachment'
) or die "Error adding $file_gif: $!\n";

### Add the ZIP file
$msg->attach (
   Type => 'application/zip',
   Path => $my_file_zip,
   Filename => $your_file_zip,
   Disposition => 'attachment'
) or die "Error adding $file_zip: $!\n";

### Send the Message
MIME::Lite->send('smtp', $mail_host, Timeout=>60);
$msg->send;

-------------
Cuvou.com | The NEW Kirsle.net
 
Thanks, so I got this ready for email, but how do I combine it with the other script?

Code:
#!C:\Perl\bin\perl.exe

use MIME::Lite;
use Net::SMTP;

### Adjust sender, recipient and your SMTP mailhost
my $from_address = 'myemail@yahoo.com';
my $to_address = 'myemail@yahoo.com';
my $mail_host = 'mailserver.com';


### Adjust subject and body message
my $subject = 'Perl Email Attachment Test';
my $message_body = "Attached is the test file.";



### Create the multipart container
$msg = MIME::Lite->new (
  From => $from_address,
  To => $to_address,
  Subject => $subject,
  Type =>'multipart/mixed'
) or die "Error creating multipart container: $!\n";



### Add text to message body
$msg->attach (
  Type => 'TEXT',
  Data => $message_body
) or die "Error adding the text message part: $!\n";



### Attach the TXT file
my $filename = 'C:\output.txt';

$mime_msg->attach(
           Type     => "application/txt",
           Encoding => "base64",
           Path     => $filename,
           Filename => "output.txt"
	   Disposition => 'attachment'
) or die "Error adding $filename: $!\n";


### Send the Message
MIME::Lite->send('smtp', $mail_host, Timeout=>60);
$msg->send;
 
Hi,

This script has been working great, but I have one question...sometimes, when two backup jobs are running at the same time, and one completes before the other, it grabs the wrong session to output into the text file. Does anyone know how to fix this?

Code:
#!C:\Perl\bin\perl.exe

open (IN, "omnidb -session -latest -detail|") or die "$!";
open(OUT, ">output.txt") or die "$!";
while (<IN>)
{
print OUT $_ ;
}
 
So here's what its doing now to be more specific:

1) Job 1 finishes.
2) Job 2 is still running.
3) Script runs and grabs output from latest job after job 1
finishes.
4) Since Job 2 is still running, it grabs the output from
that and not job 1.
 
maybe because you are writing to the same file.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
It only 'overwrites' when it opens the file. So if you have two of these bad boys running, you may even get the output from the two interleaved in the file...

Suggest you have your script make up a name for the file, perhaps using some literal value catenated to the date, time, PID, or similar. Then if you have two running, you wouldn't get a naming collision unless you were REALLY unlucky.
Code:
#!/usr/bin/perl
use warnings;
use strict;

my $file = "output_" . time() . ".txt";

print $file;
If you are this unlucky, don't worry about it, as you'll probably get run over by 13 steamrollers on your way home from work...

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Ok, that sounds right. I'm not sure if I changed this the right way though:

Code:
#!C:\Perl\bin\perl.exe
use warnings;
use strict;

my $file = "output_" . time() . ".txt";

open (IN, "omnidb -session -latest -detail|") or die "$!";
open(OUT, ">$file") or die "$!";
while (<IN>)
{
print OUT $_ ;
}
 
That looks fine. Just remember to pick up the $file variable when you are attaching it to the email...

BTW, the shebang #!/my/path/to/perl is a *nix thing to allow you to run executable scripts without specifying the interpreter. So instead of typing perl myscript you can just type ./myscript. It's a bit of unix magic that just gets treated as a comment under Windows.

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Oh crap...I forgot about the emailing. I wasn't actually using perl to email, I couldn't get it working. I was using vbscript, but it was always the same file to email. With this, it will always be a different file.
 
Tsk, tsk, how frightfully inopportune. 'Outed' as a VBScript user by your own admission, on the world's premier perl forum. Whatever next?

[smile]

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Haha well I was doing vbscript before trying perl...but I am slowly transitioning over to perl as I learn more about it. I'm just a n00b right now : P
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top