input to the program with all the strings in an array one by one

前端 未结 2 949
说谎
说谎 2020-12-20 09:54

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

相关标签:
2条回答
  • 2020-12-20 10:13

    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
        }
    }
    
    0 讨论(0)
  • 2020-12-20 10:24

    In bash, line by line:

    while read fruit
    do
      perl test.perl "$fruit"
    done < names.txt
    
    0 讨论(0)
提交回复
热议问题