Monday 15 March 2010

stdin - Asking for other inputs after piping a file to a perl script from a shell script -



stdin - Asking for other inputs after piping a file to a perl script from a shell script -

i working on school project involves having main shell script prompt user text file contains 50 words. if the shell script finds file in same directory shell , perl scripts, print menu asking if user wants sort list using shell , outputting sorted list new file (that 1 finished , works), create phone call perl script, perl script take file , print words in file, prompt user word want search for. homecoming line word on in list. have done if user selects sort using perl script, pipe inputted file in shell perl script with:

cat $filename | ./search.pl

which happens pipe file on perl script can utilize it. first while loop access list , print every word/line user see, works fine. but run trouble. after whole list printed, printf line asks word want search print, programme stop without allowing anymore input, , go terminal. logic search script print every word user see can search for, , inquire them want search for, , through inputted file shell script while loop; if find it, print found on line, if don't find go next line, , if nail end without finding print can't found.

why unable come in more input phone call stdin , assign $word utilize in sec while loop? also, when doing sec while loop, using <> after asking different output going mess things up? if so, how create reference 1 time again file sec while loop?

#!/usr/bin/env perl $count = 1; #global variable homecoming value # of words. while (<>) { printf "$_"; } #now have opened file, printed off user see, can come in word in prompt # see line on. printf "\nplease come in word want search for\n"; $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 found."; } }

the shell script (for reference):

#!/bin/bash clear printf "hello. \nplease input filename file containing list of words use. please allow 1 word per line.\n -> " read filename printf "you have entered filename: $filename.\n" if [ -f "$filename" ] #check if file exists in current directory utilize printf "the file $filename exists. file?\n\n" else printf "the file: $filename, not exist. rerun shell script , please come in valid file it's proper file extension. illustration of mywords.txt \n\nnow exiting.\n\n" exit fi printf "main menu\n" printf "=========\n" printf "select 1 sort file using shell , output new file.\n" printf "select 2 sort file using perl , output new file.\n" printf "select 3 search word using perl.\n" printf "select 4 exit.\n\n" echo "please come in selection below" read selection printf "you have selected alternative $selection.\n" if [ $selection -eq "1" ] read -p "what phone call new file? " newfile #asks user want phone call new file have sorted list outputted sort $filename > $newfile echo "your file: $newfile, has been created." fi if [ $selection -eq "2" ] read -p "what phone call new file? " newfile2 cat $filename | ./sort.pl # > $newfile2 #put sorted list new output file user specificed newfile2 fi if [ $selection -eq "3" ] cat $filename | ./search.pl fi if [ $selection -eq "4" ] printf "now exiting.\n\n" exit fi

i have modified code shown below. understanding, have been putting comments seek avoid comments wherever not required.

code:

#!/usr/bin/env perl utilize strict; utilize warnings; #input file passing argument programme $infile = $argv[0]; #opening , reading file using filehandle $fh open $fh,'<', $infile or die "couldn't open file $infile : $!"; while (<$fh>) { printf "$_"; } # seek function shown below reset file handle position origin of file seek($fh, 0, 0); printf "\nplease come in word want search for\n"; $word = <stdin>; chomp $word; $count = 1; #global variable homecoming value of words $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 found"; } close($fh);

perl stdin piping

No comments:

Post a Comment