Bash - Redirection with wildcards

后端 未结 3 1327
春和景丽
春和景丽 2021-01-28 05:19

I\'m testing to do redirection with wildcards. Something like:

./TEST* < ./INPUT* > OUTPUT

Anyone have any recommendations? Thanks.

相关标签:
3条回答
  • 2021-01-28 05:28

    There is a program called TEST* that has to get various redirection into into called INPUT*, but the thing is there are many TEST programs and they all have a different number, e.g. TEST678. What I'm trying to do is push all the random INPUT files into all the all TEST programs.

    You can write:

    for program in TEST*            # e.g., program == 'TEST678'
    do
      suffix="${program#TEST}"      # e.g., suffix == '678'
      input="INPUT$suffix"          # e.g., input == 'INPUT678'
      "./$program" < "$input"       # e.g., run './TEST678 < INPUT678'
    done > OUTPUT
    
    0 讨论(0)
  • 2021-01-28 05:31

    Say you have the following 5 files: TEST1, TEST1, INPUT1, INPUT2, and OUTPUT. The command line

    ./TEST* < ./INPUT* > OUTPUT
    

    will expand to

    ./TEST1 ./TEST2 < ./INPUT1 ./INPUT2 > OUTPUT.
    

    In other words, you will run the command ./TEST1 with 2 arguments (./TEST2, ./INPUT2), with its input redirected from ./INPUT1 and its output redirected to OUTPUT.

    To address what you are probably trying to do, you can only specify a single file using input redirection. To send input to TEST from both of the INPUT* files, you would need to use something like the following, using process substitution:

    ./TEST1 < <(cat ./INPUT*) > OUTPUT
    

    To run each of the programs that matches TEST* on all the input files that match INPUT*, use the following loop. It collects the output of all the commands and puts them into a single file OUTPUT.

    for test in ./TEST*; do
        cat ./INPUT* | $test
    done > OUTPUT
    
    0 讨论(0)
  • 2021-01-28 05:50
    for test in ./TEST*; do
     for inp in ./INPUT*; do
       $test < $inp >> OUTPUT
     done
    done
    
    0 讨论(0)
提交回复
热议问题