I'm trying to have a simple script that takes in commands both from the command line or from a batch file (if the command line directs) to pass to some lab equipment. The batch files can be nested (i.e. call other batch files.)
I've tried a simple testcase but it is not running as I desire/expect. It seems to process all of the filehandle input before starting the next.
Any suggestions? I could go full unix like with read, select etc., but I switched from C to PERL to make the user interface processing easier...
Thanks
<bold>Update:</bold>
Ken's answer does what I need and as most correct answers are, is simple and elegant. As for shift vs. pop, I definately want pop -effect- since I want a LIFO for the recursive batch processing. I say -effect- since as Ken's answer uses, recursion is a better tool for this problem.
Thanks to all who contemplated this for me.
#!/usr/bin/perl -w use strict; use warnings; my @batch; my @batchinfo; my $done = 0; my $file; my $line; my $fh = *STDIN; my $prevfh; my $curIO = "STDIN"; my $prevIO; while (!$done) { print STDOUT "] "; $line = <$fh>; if (defined($line)) { print $line; if ($line =~ /B\s+([\w\.\/\-\_]+)/) { $prevfh = $fh; $prevIO = $curIO; $file = $1; print "INFO: Opening batch file $file\n"; if (open($fh, '<:encoding(UTF-8)', $file)) { push(@batch, $prevfh); push(@batchinfo, $prevIO); $curIO = $file; } else { warn "Could not open file '$file' $!"; $fh = $prevfh; $curIO = $prevIO; } } } else { # end of file, restore if any if (@batch) { $fh = pop(@batch); $curIO = pop(@batchinfo); print STDERR "INFO: Current IO: $curIO\n"; } else { die "No other input. Ending."; $done = 1; } } }
Sample input: tst.bat
a b c B tst2.bat 1 2 3
Sample input tst2.bat
z y x w
Example output:
$ ./perl/tst.pl ] m m ] n n ] o o ] B tst.bat B tst.bat INFO: Opening batch file tst.bat ] a ] b ] c ] d ] e ] B tst2.bat INFO: Opening batch file tst2.bat ] 1 ] 2 ] 3 ] z ] y ] x ] w ] INFO: Current IO: tst.bat ] INFO: Current IO: STDIN ] No other input. Ending. at ./perl/tst.pl line 41, <STDIN> line 17.
The output I want is for the z,y,x.w entries of tst2.bat to be processed before the 1,2,3 of tst.bat. Additionally when this finishes with the file inputs, it treats STDIN as if its at EOF, and exits indicating 'No other input', even though I have not pressed ctrl-D.
Thanks!
skorson
In reply to [SOLVED] Interleave STDIN and FILE IO by skorson
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |