<> Some commands do not support input streams

We talked about it earlier , Some commands do not support input streams , Only command line parameters are supported , Such as the most commonly used ls. We usually use it like this
$ ls /var/ backups cache crash lib local lock log mail metrics opt run snap
spool tmp
But it can't be used like this
$ echo "/var" | ls
If used in this way ,ls The input stream passed to it by the pipe is ignored . In the end, it's equivalent to , Only run the ls.

<>xargs The use of

that , If the command line or script does need to use a similar writing , What's the solution ? The answer is to use xargs,xargs
One of the functions of is to convert pipe or standard input data into command line parameters , for example
$ echo "/var" | xargs ls backups cache crash lib local lock log mail metrics
opt run snap spool tmp
above , One more xargs , You can convert the output of the pipeline to ls Parameters of .

<> Principle analysis

The original function of the pipe is to connect echo Standard output to ls Standard input for , Added xargs, It's like a pipe connection echo Standard output to xargs
Standard input for ,xargs The standard input is converted into the parameters of the command , Pass to command .

<> Source code analysis

Source code first

# define READ_ARGS(l, e, nmc, mc) process_stdin(l, e, nmc, mc) int xargs_main(
int argc, char **argv) { while ((list = READ_ARGS(list, eof_str, n_max_chars,
max_chars)) != NULL || /* Read data from standard input as a parameter list */ (opt & OPT_NO_EMPTY) == 0) { opt |=
OPT_NO_EMPTY; n = 0; n_chars = 0; /* allocating pointers for execvp: a*arg,
n*arg from stdin, NULL */ args = xcalloc(n + a + 1, sizeof(char *)); /* Store
the command to be executed (taken from the command line) */ for (i = 0; i < a; i
++) args[i] = argv[i]; /* (taken from stdin) */ for (cur = list; n; cur = cur->
link) { args[i++] = cur->data; n--; } if ((opt & OPT_INTERACTIVE) == 0 ||
xargs_ask_confirmation() != 0) { child_error = xargs_exec(args); /* Execute the order , among
args The value of may be : args[0] = "ls", args[1] = "-a" */ } /* clean up */ for (i = a; args[
i]; i++) { cur = list; list = list->link; free(cur); } free(args); if (
child_error> 0 && child_error != 123) { break; } } return child_error; }
therefore ,xargs The principle of this method is to read the standard input , Convert to parameter list , With the help of exec function , Execute commands and parameters .

<> promote

We found that , adopt xargs Executive ls command , Lost color , That's because in the shell Execution in ls, In fact, it is ls --color=auto,ls yes
alias to ls --color=auto The alias of . If we want our output to be colored , You can use the following command :

Ah , The color is back

©2019-2020 Toolsou All rights reserved,
What is a process , Concept of process ? Tang Seng team to lay off staff , Who will you lay off ?Oracle Database access performance optimization JVM Old age garbage collection Full GCweb Two front-end practical games ( Attached source code ) about linux command “iptables -F”, Don't carry it out easily navicat function sql File error 2021-06-03 A man is not born to be defeated Pandas And openpyxl Super combination of Library , bye ,Excel! It is never recommended to spend a lot of time studying for employment python