using linux how can I pass the contents of a file

2020-05-20 02:11发布

let's say I want to run ./program with a string argument

instead of typing ./program string each time, how can I do ./program <file> where <file> is a file that contains string?

thankyou

标签: linux
6条回答
闹够了就滚
2楼-- · 2020-05-20 02:27

Command

./program "$(< file)"

Explanation

  1. $(cmd) runs a command and converts its output into a string. $(cmd) can also be written with backticks as `cmd`. I prefer the newer $(cmd) as it nests better than the old school backticks.

  2. The command we want is cat file. And then from the bash man page:

    The command substitution $(cat file) can be replaced by the equivalent but faster $(< file).

  3. The quotes around it make sure that whitespace is preserved, just in case you have spaces or newlines inside of your file. Properly quoting things in shell scripts is a skill that is sorely lacking. Quoting variables and command substitutions is a good habit to good into so you don't get bit later when your file names or variable values have spaces or control characters or other weirdnesses.

查看更多
神经病院院长
3楼-- · 2020-05-20 02:29

Another option is xargs:

cat file | xargs ./program

I personally find this version easier to read, especially if you're doing more than just catting a file.

From the man page:

xargs reads items from the standard input, delimited by blanks or newlines, and executes the command one or more times with any initial-arguments followed by items read from standard input.

In plain speak, xargs will execute ./program on each line/word that is being piped to it.

See man xargs for more options.

查看更多
别忘想泡老子
4楼-- · 2020-05-20 02:35

This should do the trick:

./program `cat file`

If you want the entire file content in a single argument, add double quotation (tested in bash. I think It may vary shell to shell) :

./program "`cat file`" 
查看更多
The star\"
5楼-- · 2020-05-20 02:40

This is extension to @robbrit answer.

Consider a file.txt with content:

1 2 3
4 5 6

And the a program.c with content:

#include<stdio.h>
void main(int argc, char** argv) 
{ 
    for (int i = 0; i < argc; i++) 
        printf("Arg %d: %s\n", i, argv[i]); 
}

Here is what happen if you pass command line arguments with quotes and without:

$ gcc  program.c -o program

$ ./program `cat file`
Arg 0: ./program
Arg 1: 1
Arg 2: 2
Arg 3: 3
Arg 4: 4
Arg 5: 5
Arg 6: 6

$ ./program "`cat file`"
Arg 0: ./program
Arg 1: 1 2 3
4 5 6
查看更多
狗以群分
6楼-- · 2020-05-20 02:46

All the above answers work but I find the cleanest and easiest to type option is

xargs < file ./program
查看更多
贪生不怕死
7楼-- · 2020-05-20 02:52

You can use one of:

./program $(cat file)
./program "$(cat file)"

The latter is useful if there may be multiple words in the file and you want them to be treated as a single argument. In any case, I prefer the use if $() to backticks simply due to it's ability to nest (which is probably not a requirement in this case).

Also keep in mind that this answer (and others) are more related to the shell in use rather than Linux itself. Since the predominant and the best :-) shell seems to be bash, I've coded specifically for that.

This is actually a fairly common way of killing processes under UNIX lookalikes. A daemon (like cron) will write its process ID to a file (like /var/cron.pid) and you can signal it with something like:

kill -HUP $(cat /var/cron.pid)
查看更多
登录 后发表回答