0

I have a shell script consisting of so many perl scripts, one of the perl script have to be run with differnt input each time and the value has to be stored in a single file at the end as shown

 #!/bin/sh
 .....
 ....
 perl test.pl apple
 perl test.pl mango
 perl test.pl banana
 ... 
... 
....

I type these names in command lines by looking at the file generated with these names.

**names.txt**
apple
mango
banana

OR

**names.txt**
    apple    mango   banana

Is their a way in perl or shell which takes each name at a time as an input.
That is can names.txt be considered as an array and then the perl script take each array value at a time as an input using ARGV or any other means.So that i can have my shell script like

#!/bin/sh
     .....
     ....
     perl test.pl names.txt
     .... 
    ... 
    ....

2 Answers 2

1

The usual way to write perl programs that take input from files is to construct them as follows:

while ($line = <>) {

    # do stuff with $line
}

If filenames are given on the command line, perl will automatically open them one by one, feeding the lines to your script. If no filenames are given, it will read from standard input.

But if you write your script this way, you won't be able to give it fruits directly on the command line, they will have to be in a file or standard input.

To handle multiple fruit on the same line, your code can do:

while (my $line = <>) {

    chomp $line;

    foreach my $fruit ( split ' ', $line ) {

        # do something with $fruit
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

open FILE2, "<31.txt"; open FILE3, ">>file2.l"; open FILE123, "<names.txt"; my $names; while (my $line=<FILE2>) { if ($line =~ m/**$names**/ig) .... .... .... ... Here is my code and $names will have to given different inputs taken from names.txt each time.
Your code goes where I wrote # do something with $fruit. Just change the name of the variable from $names to $fruit. Or maybe you could read all the fruit names first, and put them into an array and use that in your code. I'm just showing you the general way to process a file given on the command line, you can do with it whatever you like.
0

In bash, line by line:

while read fruit
do
  perl test.perl "$fruit"
done < names.txt

9 Comments

I am new to programming would u please tel me what bash is?? Am i supposed to write these commands in my shell script????
bash is a Bourne-shell-compatible shell. Use #!/bin/bash as your shebang line (assuming that's where bash is) (although this construct might work fine with sh).
Ya i got that... And do i have to use ARGV[$i] in the place the perl script uses that input given in command line?????
The lines will be passed to the script one at a time, just as you asked for.
My code runs when the name is given in the command line but when i use these set of line it gives me undesirable output... According to the output i am getting i feel that the perl script isnt taking the input value it needs to take.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.