i have a perl script(myscript.pl)
use strict;
use IO::Handle;
open OUTPUT, '>', "output.txt" or die $!;
STDOUT->fdopen( \*OUTPUT, 'w' ) or die $!;
while (<STDIN>){
print $_;
}
i can paste into output.txt what i write on commandline
also
I have a bash script:
cat >in.$$
cat in.$$> /tmp/msg
i can paste what i have in in.$$ into /tmp/msg
i need to run perl script with stdin that come from bash script.
is it possible and how?
i tried , even though i know it is too silly
cat in.$$> ./myscript.pl
in.$$> ./myscript.pl
in.$$> perl myscript.pl
To specify the contents of a file as standard input to a bash command, use <
./myscript.pl < in.$$
To use the output of one command as the input to another command, use '|' (the 'pipe' character, as if you were sending bits from one process to another through a pipe)
cat in.$$ | ./myscript.pl
echo foo | ./myscript.pl
In your Perl script, use the diamond operator (aka the null file handle ):
The null filehandle
<>
is special: it can be used to emulate the behavior ofsed
andawk
, and any other Unix filter program that takes a list of filenames, doing the same to each line of input from all of them. Input from<>
comes either from standard input, or from each file listed on the command line. Here's how it works: the first time<>
is evaluated, the@ARGV
array is checked, and if it is empty,$ARGV[0]
is set to "-", which when opened gives you standard input. The@ARGV
array is then processed as a list of filenames.
Here's an example:
#! /usr/bin/perl
# cat command emulator
while ( my $line = <> ) {
chomp $line;
say $line;
}
The <>
will take each file listed on the command line, open them sequentially, and then read each line, or pull lines in from STDIN.
You can also do this:
while ( my $line = <STDIN> ) {
chomp $line;
say $line;
}
Which will read in lines from STDIN, but won't read files from the command line.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.