UNIX: How to run a program with a file as an input

后端 未结 2 678
眼角桃花
眼角桃花 2021-01-14 13:16

I\'m writing a bash script called \'run\' that tests programs with pre-defined inputs.

It takes in a file as the first parameter, then a program as a second paramete

2条回答
  •  时光取名叫无心
    2021-01-14 13:37

    The < operator redirects the contents of the file to the standard input of the program. This is not the same as using the file's contents for the arguments of the file--which seems to be what you want. For that do

    ./program $(cat file.txt) 
    

    in bash (or in plain old /bin/sh, use

    ./program `cat file.txt`
    

    ).

    This won't manage multiple lines as separate invocations, which your edit indicates is desired. For that you probably going to what some kind scripting language (perl, awk, python...) which makes parsing a file linewise easy.

提交回复
热议问题