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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to automate a couple scripts 1

Status
Not open for further replies.

PerlGerl

Programmer
Jun 29, 2004
19
US
I am looking to run a few scripts within a script, if possible. I wrote a few scripts that parse text from different files, what I want to do is link them so I can run one script that will run all other scripts. like
script1.pl
script2.pl
script3.pl
and script4.pl will run all three, automating the process of having to do each separatly.

I could do it by pasting the majority of the code from all three scripts into script4 and just use package variables, but I dont want to do that. let me know if this makes sense
 
can you just do this:

Code:
#!/usr/local/bin/perl -w

./script1.pl
./script2.pl
./script3.pl


___________________________________
[morse]--... ...--[/morse], Eric.
 
Check out do, in particular the do EXPR section.

perldoc -f do
 
Hi PerlGerl

You should probably use a cron job

Type crontab -e at the UNIX command line and you'll likely be thrown into vi or pico

[blue][tt]quick notes on [red]vi[/red] - VERY basic!

once loaded you can press [red]i[/red] to enter [red]insert[/red] mode

[red]Esc[/red] this 'readies' vi for a command - once pressed you can...
[red]:w[/red] to 'write' out (save) the file, or...
[red]:q[/red] quit, or...
[red]:wq[/red] write and then quit[/tt][/blue]


if you haven't got any cron jobs there already you'll have to create one:-

[tt]00 12 * * * /usr/bin/perl /home/mywebspace/cgi-bin/perlscript.cgi | /usr/sbin/sendmail you@yourisp.com[/tt]

the syntax for the 5 fields at the start of the cron job are what control the exact time of execution:-

[tt]Minute (0-59) Hour (0-23) Day of Month (1-31) Month (1-12 or Jan-Dec) Day of Week (0-6 or Sun-Sat) Command

0 2 12 * 0,6 /usr/bin/find

This line executes the "find" command at 2AM on the 12th of every month that a Sunday or Saturday falls on[/tt]


* are wildcards and match every available option

This script will execute at mid-day, each day of the week

Add further lines with the same syntax to do further jobs. You can enter as many lines as you like

Be sure to include the full path of the applications in your cron jobs - it is not forgiving

I've tried to make this as concise and understandable as possible - sorry if you already know it!

Good Luck :)


Kind Regards
Duncan
 
Perhaps I should have been more clear, It is not as simple as running the three scripts in succession. The second script takes parameters from the command line(a file name to parse). I am thinking first glob the files needed from the directory then do a loop to iterate thru each file and pass as @ARGV to the second script. The third script does the same but can take all the files names without needing a loop foreach name. The loop and the globbing I am fine with, its the "how to execute scripts" part I am unsure of how to do.
 
can't you save the file at the end of each script to a temporary file on the hard disk?


Kind Regards
Duncan
 
Maybe its worth thinking about building modules out of the scripts?
 
modules......dun dun dun.....*gasp*

Thats actually a great idea.

___________________________________
[morse]--... ...--[/morse], Eric.
 
do EXPR seems like the best option, i can't seem to make it work. Here is my code, it doesnt do anything but you can see what I am trying to do. I tried making modules and maybe im not doing it right, but all i can do is inherit variables and functions, but not excute the code as a script. do EXPR does this but, maybe i am impletmenting it wrong.

Code:
### excecute script1.pl
do "script1.pl";

### excecute script2.pl on each .c file in the directory
@event_files = <*.c>;
@event_files  = glob("*.c");
	   
foreach $event_files  (@event_files) {
	@ARGV = $event_files;   
	do "script2.pl";
}

### excecute script3.pl on each .xml file in the directory
@xml_files = <*.c>;
@xml_files = glob("*.xml");
	@ARGV = $xml_files;
	do 'script3.pl';

ideas?
 
I don't know anything about this but will this work...

[tt]do 'script3.pl @ARGV';[/tt]


Kind Regards
Duncan
 
do 'script3.pl @ARGV';

does not work :( also I noticed that I that I had double quotes, I changed those to single quotes, no dice still.
 
I got do to work, if anyone is interested.

Code:
### vars
$file1 = "script1.pl";
$file2 = "script2.pl";
$file3 = "script3.pl";

### run script1.pl complain if can't run
unless ($return = do $file1) {
    warn "couldn't parse $file1: $@"         if $@;
    warn "couldn't do $file1: $!"            unless defined $return;
    warn "couldn't run $file1"               unless $return;
}

### run script2.pl on each .c file in the directory complain if can't run
@c_files = <*.c>;
@ARGV = @c_files;
foreach $ARGV  (@ARGV) {
	unless ($return = do $file2) {
   warn "couldn't parse $file2: $@"         if $@;
	warn "couldn't do $file2: $!"            unless defined $return;
   warn "couldn't run $file2"               unless $return;
}}


### excecute script3.pl on each .xml file in the directory
@xml_files = <*.xml>;
	@ARGV = @xml_files;
	unless ($return = do $file3) {
	warn "couldn't parse $file3: $@"         if $@;
	warn "couldn't do $file3: $!"            unless defined $return;
	warn "couldn't run $file3"               unless $return;
	}
 
That is interesting. When I suggested do, I didn't know scripts2-3 were going to need to access @ARGV. So they're accessing the @ARGV of the main script that's "do-ing" them? That's impressive. How are they accomplishing this, just in the usual way?

perldoc says
"do FILENAME" cannot see lexicals in the enclosing scope;
which implies that it can see non-lexicals like @ARGV. Hmm ... You're not using my in your main script. Can scripts1-3 see the other variables in the main script, since you're not using my?
 
So they're accessing the @ARGV of the main script that's "do-ing" them? That's impressive. How are they accomplishing this, just in the usual way?

scripts2-3 work on their own to parse files and output a new file. They each take the desired files as arguments but you can enter as many filenames as arguments as you want and it will parse each, any it doesn't find it warns and continues. What I did was to glob the files i wanted and passed them to the @ARGV that scripts2-3 are expecting. This way the script executes once and doesn't go into a nasty loop looking for each $ARGV.


Can scripts1-3 see the other variables in the main script, since you're not using my?

Yes they can. Just tired it with a simple string to verify it passed to script3 without issue. As lexical variables are privatized in some scope, it makes sense that non-lexicals can be called.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top