473,624 Members | 2,150 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

writing to a single file from two scripts

4 New Member
Hi,

I'm new to perl.
I have 4 scripts which do some operations and then should write to a single file with the output.
the first script calls the 2nd script and the 2nd one calls the third and fourth scripts.
I want to write the output to a single file "log.txt".
i'm using statements like - "print LOG "Installati on not successful!!\n" ;" - to write to the file. Here LOG is the file handle.
Problem is that the script does not write anything to the file.
Appreciate any help/pointers.
Dec 6 '07 #1
8 1687
eWish
971 Recognized Expert Contributor
Please post the code you have tried so that we can help you.

--Kevin
Dec 6 '07 #2
imsandy
4 New Member
Hi,
The code snippets -

the main.pl file- this calls another script called cp_script.pl
Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.     $datetime = localtime();                 
  3.     print $datetime;                         
  4. # open a log file to write to
  5. $LOG="logfile";
  6. $log_file = ">log.dat";
  7.         open(LOG, $log_file) ||die("could not open file - log.txt!\n");
  8. print LOG "=====================\n";
  9. print LOG "=====================\n";
  10. print LOG "$datetime\n";
  11. print LOG "   ------   \n";
  12. print LOG "--- main.pl script ---\n";
  13. #list the package names and put them in a file
  14. $res_cp = `./cp_script.pl`;
  15. if ($res_cp != 0) {
  16.         print LOG "Error in running script cp_script.pl\n\n";
  17.         print LOG "Exiting...\n";
  18.         exit(1);
  19. }
  20. print LOG "\n--- End of mail.pl scirpt ---\n";
  21. close(LOG);
  22.  
  23. the file cp_script.pl -
  24. $LOG="logfile";
  25. $log_file = ">log.dat";
  26.         open(LOG, $log_file) ||die("could not open file - log.txt!\n");
  27.  
  28. # copy pkg.dat
  29. $res =`cp pkg.dat ./sanity/pkg.dat`;
  30. if ($res != 0) {
  31.         print LOG "unable to copy the pkg.dat file";
  32.         print LOG "Exiting...\n";
  33.         exit(1);
  34. }
  35. print LOG "\n--- End of cp_script.pl scirpt ---\n";
  36. close(LOG);
Dec 6 '07 #3
eWish
971 Recognized Expert Contributor
Please do yourself a favor and add the following lines of code to your script after the shebang. Then fix the errors and report back.

Expand|Select|Wrap|Line Numbers
  1. use strict;
  2. use warnings;
Also declare your variables. This will help debug your script easier.

--Kevin
Dec 6 '07 #4
imsandy
4 New Member
Please do yourself a favor and add the following lines of code to your script after the shebang. Then fix the errors and report back.

Expand|Select|Wrap|Line Numbers
  1. use strict;
  2. use warnings;
Also declare your variables. This will help debug your script easier.

--Kevin
Thanks a lot Kevin.
It works now.
Dec 6 '07 #5
imsandy
4 New Member
Hi,

I am facing a problem with the sequence of the logs.
The logs for the script which was called are printed before the calling script's logs. Also the logs are printed only when the script exits.
Is there any way in which this can be changed to the correct sequence?

The modified scripts -
main.pl
Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. my $datetime = localtime();                 
  5. my $res_cp = 0;
  6.     print $datetime;                         
  7. # open a log file to write to
  8. my $LOG="logfile";
  9.  
  10. my $log_file = ">>log.dat";
  11.  
  12.         open(LOG, $log_file) ||die("could not open file - log.txt!\n");
  13.  
  14. print LOG "=====================\n";
  15. print LOG "=====================\n";
  16. print LOG "$datetime\n";
  17. print LOG "   ------   \n";
  18. print LOG "--- main.pl script ---\n";
  19.  
  20. #list the package names and put them in a file
  21. $res_cp = `./cp_script.pl`;
  22. close(LOG);
  23.  
cp_script.pl
Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3. use strict;
  4. use warnings;
  5.  
  6. my $LOG;
  7. my $log_file;
  8. my $res=1;
  9.  
  10. $LOG="logfile";
  11.  
  12. $log_file = ">>log.dat";
  13. open(LOG, $log_file) ||die("could not open file - log.txt!\n");
  14.  
  15. print LOG "opened the log file\n";
  16. # copy pkg.dat
  17. print LOG "copying the pkg.dat file\n";
  18. $res =`cp pkg.dat ./sanity/pkg.dat`;
  19.  
  20. $res = `./next.pl`;
  21. print LOG "\n--- End of cp_script.pl scirpt ---\n";
  22.  
  23. close(LOG);
  24.  
next.pl
Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4.  
  5. my $LOG="handle";
  6. my $filename = ">>log.dat";
  7. open(LOG, $filename) ||die("could not open file - log.txt!\n");
  8.  
  9. print LOG "in next.pl\n";
  10.  
output :-
>cat log.dat
in next.pl
opened the log file
copying the pkg.dat file

--- End of cp_script.pl scirpt ---
=============== ======
=============== ======
Wed Dec 12 00:04:14 2007
------
--- main.pl script ---
Dec 12 '07 #6
numberwhun
3,509 Recognized Expert Moderator Specialist
Hi,

I am facing a problem with the sequence of the logs.
The logs for the script which was called are printed before the calling script's logs. Also the logs are printed only when the script exits.
Is there any way in which this can be changed to the correct sequence?

The modified scripts -
main.pl
Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. my $datetime = localtime();                 
  5. my $res_cp = 0;
  6.     print $datetime;                         
  7. # open a log file to write to
  8. my $LOG="logfile";
  9.  
  10. my $log_file = ">>log.dat";
  11.  
  12.         open(LOG, $log_file) ||die("could not open file - log.txt!\n");
  13.  
  14. print LOG "=====================\n";
  15. print LOG "=====================\n";
  16. print LOG "$datetime\n";
  17. print LOG "   ------   \n";
  18. print LOG "--- main.pl script ---\n";
  19.  
  20. #list the package names and put them in a file
  21. $res_cp = `./cp_script.pl`;
  22. close(LOG);
  23.  
cp_script.pl
Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3. use strict;
  4. use warnings;
  5.  
  6. my $LOG;
  7. my $log_file;
  8. my $res=1;
  9.  
  10. $LOG="logfile";
  11.  
  12. $log_file = ">>log.dat";
  13. open(LOG, $log_file) ||die("could not open file - log.txt!\n");
  14.  
  15. print LOG "opened the log file\n";
  16. # copy pkg.dat
  17. print LOG "copying the pkg.dat file\n";
  18. $res =`cp pkg.dat ./sanity/pkg.dat`;
  19.  
  20. $res = `./next.pl`;
  21. print LOG "\n--- End of cp_script.pl scirpt ---\n";
  22.  
  23. close(LOG);
  24.  
next.pl
Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4.  
  5. my $LOG="handle";
  6. my $filename = ">>log.dat";
  7. open(LOG, $filename) ||die("could not open file - log.txt!\n");
  8.  
  9. print LOG "in next.pl\n";
  10.  
output :-
>cat log.dat
in next.pl
opened the log file
copying the pkg.dat file

--- End of cp_script.pl scirpt ---
=============== ======
=============== ======
Wed Dec 12 00:04:14 2007
------
--- main.pl script ---
Kevin was kind enough to add the code tags to your first code posting and I have now done it for your second. Any time you post code in the forums, the code NEEDS to be surrounded by the proper code tags.

This is a warning. Please be sure and use code tags so we don't have to clean up behind you. They are required for all posted code.

Regards,

Jeff
Dec 12 '07 #7
KevinADC
4,059 Recognized Expert Specialist
imsandy,

why do you have all that as seperate script? Why not combine it all into one script?
Dec 12 '07 #8
jagjot
6 New Member
Hi

You can open ur output logfile in append mode when u have to dump the results for 2nd, 3rd n 4th scripts and then print in that OUTFILE . For the first script's output, u can open it in simple write mode.
Dec 13 '07 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

2
1550
by: el_roachmeister | last post by:
I wonder do most php coders use ftp or ssh (terminal login) to write php scripts? I use ssh because my host offers it, but I notice most hosts that charge less than $10/month don't offer ssh. Are there are any tools one can use to write long scripts (>5000 lines of code)?
48
8460
by: Joseph | last post by:
Hi I'm writing a commercial program which must be reliable. It has to do some basic reading and writing to and from files on the hard disk, and also to a floppy. I have foreseen a potential problem. The program may crash unexpectedly while writing to the file. If so, my program should detect this during startup, and then (during startup) probably delete the data added to the file and redo the writing operation.
1
3172
by: Craig Ringer | last post by:
Hi folks I'm a bit of a newbie here, though I've tried to appropriately research this issue before posting. I've found a lot of questions, a few answers that don't really answer quite what I'm looking for, but nothing that really solves or explains all this. I'll admit to being stumped, hence my question here. I'm also trying to make this post as clear and detailed as possible. Unfortunately, that means it's come out like a book. I...
4
4015
by: George Stout | last post by:
First off I do not know alot about writing queries to an Access Database from an ASP page. This is why I need help. I have an Events database for 6 colleges in our metro area. On the homepage I have to display the next event for each college. That would give me 6 events listed on the page. I have been trying to figure out how to write a query statement in my ASP page to select just the most current event from each college. I have not had...
7
1518
by: John Pote | last post by:
Hello, help/advice appreciated. Background: I am writing some web scripts in python to receive small amounts of data from remote sensors and store the data in a file. 50 to 100 bytes every 5 or 10 minutes. A new file for each day is anticipated. Of considerable importance is the long term availability of this data and it's gathering and storage without gaps. As the remote sensors have little on board storage it is important that a
13
4264
by: Kevin Walzer | last post by:
Which of the Windows/Unix package builders for Python applications is capable of creating single-file executables? I'm thinking of: 1. py2exe 2. Mcmillan Installer/PyInstaller 3. cxfreeze The apps I've seen created by py2exe aren't single-file at all, the install folder is full of files besides the main program. I'm looking for a solution that stuffs all libraries, scripts, and the Python
4
3132
by: abrtlt | last post by:
I read in Programming PHP (O'Reilly) that flock() "cannot prevent two PHP scripts running in the same web server process from accessing a file at the same time". In my case a single PHP script appends text strings to an existing text file. Several clients may trigger the same script on the same web page and thus more than one PHP instance might try to open and append text to the same file at the same time. I would assume that using...
6
5259
by: arne.muller | last post by:
Hello, I've come across some problems reading strucutres from binary files. Basically I've some strutures typedef struct { int i; double x; int n; double *mz;
0
1245
by: xahlee | last post by:
Here's a little tutorial that lets you write emacs commands for processing the current text selection in emacs in your favorite lang. Elisp Wrapper For Perl Scripts http://xahlee.org/emacs/elisp_perl_wrapper.html plain text version follows. ------------------------------------- Elisp Wrapper For Perl Scripts
0
8679
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8621
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8335
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7159
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5563
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4079
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4174
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2606
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1785
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.