在将文件从shell脚本管道传输到perl脚本后,请求其他输入



我正在做一个学校项目,该项目涉及一个主shell脚本,该脚本将提示用户输入一个包含50个单词的文本文件。如果shell脚本在与shell和perl脚本相同的目录中找到该文件,它将打印一个菜单,询问用户是否希望使用shell对列表进行排序,并将排序后的列表输出到一个新文件(该文件已完成并有效),调用perl脚本,在那里perl脚本将获取该文件并打印该文件中的所有单词,然后提示用户要搜索的单词。这将返回单词在列表中的哪一行。我所做的是,如果用户选择使用perl脚本进行排序,我们将shell中输入的文件通过管道传输到perl脚本,并使用:

cat$filename|/search.pl

这恰好成功地将文件传输到perl脚本,在那里我们可以使用它。第一个while循环是我们访问列表并打印每个单词/行供用户查看的地方,这很好但这就是我遇到麻烦的地方。打印完整个列表后,将打印请求他们要搜索的单词的printf行,但随后程序将停止,不再允许输入,并返回终端我对这个搜索脚本的逻辑是,我们打印每个单词,让用户看看他们可以搜索什么,然后问他们想搜索什么,再通过while循环从shell脚本中查看输入的文件;如果我们找到了它,就打印出我们在那一行找到的,如果我们没有找到它,就转到下一行,如果我们打到最后没有找到它就打印出找不到它。

为什么我无法通过对STDIN的调用输入更多输入,并将其分配给$word以在第二个while循环中使用此外,当我进行第二个while循环时,使用<gt;在要求不同的输出后自己会把事情搞砸吗?如果是,如何在第二个while循环中再次引用该文件?

#!/usr/bin/env perl
$count = 1;    #global variable for return value
               # of words.
while (<>) {
    printf "$_";
}
#Now that we have opened the file, printed off everything for the user to see, they can now enter a     word in a prompt to
# see what line it is on.
printf "nPlease enter the word you want to search forn";
my $word = <STDIN>;
chomp $word;
while ( $line = <> ) {
    if ( $line =~ m/$word/ ) {
        print "$word has been found on line $count.nn";
    } elsif ( $line !=~ m/$word/ ) {
        $count++;
    } else {
        print "$word cannot be found.";
    }
}

Shell脚本(供参考):

#!/bin/bash
clear
printf "Hello. nPlease input a filename for a file containing a list of words you would like to use.      Please allow for one word per line.n -> "
read filename
printf "You have entered the filename: $filename.n"
if [ -f "$filename" ] #check if the file even exists in the current directory to use
then
    printf "The file $filename exists.  What would you like to do with this file?nn"
else
    printf "The file: $filename, does not exist.  Rerun this shell script and please enter a valid    file with it's proper file extension.  An example of this would be mywords.txt nnNow exiting.nn"
  exit
fi
printf "Main Menun"
printf "=========n"
printf "Select 1 to sort file using Shell and output to a new file.n"
printf "Select 2 to sort file using Perl and output to a new file.n"
printf "Select 3 to search for a word using Perl.n"
printf "Select 4 to exit.nn"
echo "Please enter your selection below"
read selection
printf "You have selected option $selection.n"
if [ $selection -eq "1" ]
then
    read -p "What would you like to call the new file? "  newfile   #asks user what they want to call   the new file that will have the sorted list outputted to it
    sort $filename > $newfile
    echo "Your file: $newfile, has been created."
fi
if [ $selection -eq "2" ]
then
    read -p "What would you like to call the new file? "  newfile2
    cat $filename | ./sort.pl
    # > $newfile2   #put the sorted list into the new output file that the user specificed with    newfile2
fi
if [ $selection -eq "3" ]
then
    cat $filename | ./search.pl
fi
if [ $selection -eq "4" ]
then
     printf "Now exiting.nn"
     exit
fi

我已经修改了您的代码,如下所示。为了你的理解,我一直在发表评论,但尽量避免在不需要的地方发表评论。

代码:

#!/usr/bin/env perl    
use strict;
use warnings;
#Input File passing as an argument to the program
my $InFile = $ARGV[0];
#Opening and reading a file using filehandle $fh
open my $fh,'<', $InFile or die "Couldn't open the file $InFile : $!"; 
while (<$fh>) {
    printf "$_";        
}
# Seek function as shown below will reset the file handle position to beginning of the file 
seek($fh, 0, 0);
printf "nPlease enter the word you want to search forn";
my $word = <STDIN>;
chomp $word;
my $count = 1; #global variable for return value of words               
my $val = 0;
while (my $line = <$fh>) {
    if ($line =~ m/$word/) {
        print "$word has been found on line $count.nn";
        $val++;
    } 
    elsif ($line !~ m/$word/) {
        $count++;
    } 
}
if ($val == 0) {
    print "$word cannot be found";
} 
close($fh); 

最新更新