我正在开展一个学校项目,该项目涉及一个主shell脚本,该脚本将提示用户输入包含50个单词的文本文件。如果shell脚本在shell和perl脚本所在的目录中找到该文件,它将打印一个菜单,询问用户是否要使用shell对列表进行排序并将排序后的列表输出到新文件(该文件已完成)并且工作),调用perl脚本,其中perl脚本将获取该文件并打印该文件中的所有单词,然后提示用户输入他们想要搜索的单词。这将返回列表中单词所在的行。我所做的是如果用户选择使用perl脚本进行排序,我们将shell中输入的文件通过以下方式传递给perl脚本:
cat $ filename | ./search.pl
将文件成功传送到我们可以使用它的perl脚本会发生什么。第一个while循环是我们访问列表并打印每个单词/行以供用户查看的地方,它工作正常。 但这是我遇到麻烦的地方。打印完整个列表后,printf行会询问要搜索的单词,然后程序将停止而不再允许输入,然后返回终端。我的逻辑是什么对于这个搜索脚本,我们打印每个单词供用户查看他们可以搜索的内容,然后询问他们想要搜索的内容,然后使用while循环查看shell脚本中输入的文件;如果我们找到它,打印我们在那条线上发现它,如果我们找不到它然后转到下一行,如果我们到达终点而没有找到它只是打印它不能找到。
为什么我无法通过调用STDIN输入更多输入并将其分配给$ word以在第二个while循环中使用?另外,当我正在执行第二个while循环时,正在使用<>在要求一个不同的输出去搞乱后,它本身?如果是这样,我如何再次引用第二个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 for\n";
my $word = <STDIN>;
chomp $word;
while ( $line = <> ) {
if ( $line =~ m/$word/ ) {
print "$word has been found on line $count.\n\n";
} 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?\n\n"
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 \n\nNow exiting.\n\n"
exit
fi
printf "Main Menu\n"
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.\n\n"
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.\n\n"
exit
fi
答案 0 :(得分:0)
我修改了您的代码,如下所示。为了您的理解,我一直在发表评论,但在不需要的地方尽量避免评论。
<强>代码:强>
#!/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 for\n";
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.\n\n";
$val++;
}
elsif ($line !~ m/$word/) {
$count++;
}
}
if ($val == 0) {
print "$word cannot be found";
}
close($fh);