Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the difference between xargs,-exec and pipes in shell

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

In this article Xiaobian for you to introduce in detail "what is the difference between xargs,-exec and pipe in shell", the content is detailed, the steps are clear, and the details are handled properly. I hope that this article "what is the difference between xargs,-exec and pipeline in shell" can help you solve your doubts.

1. Xargs function:

(1) pass the standard output of the previous command to the next command. As its parameter, the default command of xargs is echo, and the space is the default delimiter.

(2) convert multiple lines of input to a single line

2. Usage mode: front command | xargs-option latercommand

Front command: previous command

-option: options for xargs

Later command: the latter command

Usually used in conjunction with pipeline "|"

3. Common options for xargs

-n: specify the number of parameters to be processed at a time

-d: custom parameter delimiter

-p: ask whether to run the later command parameter

-t: indicates that the command is printed before execution

-I: deal with it item by item

... See man xargs for more parameters.

4. The difference between xargs and pipe |

The pipe "|" is used to pass the standard output of the previous command to the standard input of the next command.

Xargs passes the standard output of the previous command to the next command as its argument.

It can be seen that the standard input is different from the command parameters. Personal understanding, the command parameters are directly followed by the command, standard input can be keyboards, files, etc.

Therefore, the pipe character | the parameters passed to the program are not simply entered after the program name, but will be received by the internal reading functions of the program, such as scanf and gets, while xargs passes the content to the program as a normal parameter, which is equivalent to directly following the command. Moreover, some commands do not accept standard input, such as kill,rm and other commands.

Summary: not adding xargs after the pipe character is equivalent to entering the results of the previous command execution of the pipe character from the keyboard after entering the command after xargs.

After xargs is equivalent to executing the command (the command after xargs), enter the result of the command in front of the pipe character directly from the keyboard and then enter, that is, the result in front of the pipe is directly followed by the command after xargs.

To sum up, the order of enter is not quite the same.

5. The difference between xargs and-exec

-exec: {} indicates that the argument to the command is the file found, indicating the end of the comman command with;. \ is an escape character, and because the semicolon also has its use in the command, it is qualified with a\ to indicate that it is a semicolon rather than something else.

-ok: the function and format of-exec are the same, except that before executing the command given by shell in a more secure mode, a prompt is given to the user to determine whether to execute it.

# xargs passes the parameters to echo once, that is, execute: echo begin. / xargs.txt. / args.txt

Find. -name'* .txt'- type f | xargs echo begin

# exec passes one parameter at a time, that is, execute: echo begin. / xargs.txt;echo begin. / args.txt

Find. -name'* .txt'- type f-exec echo begin {}\

Xargs should be combined with pipes to complete the format: find [option] express | xargs command obviously, exec executes a command for each file found, and unless the single file name exceeds a few k, there will be no error report on the command line. And xargs is to transfer all the file names found to the command. When there are many files, the command-line parameters combined with these file names can easily be too long, resulting in command errors.

In addition, the combination of find | xargs can also make errors when dealing with file names with space characters, because the command executed at this time no longer knows which separators are separators and which are spaces in the file name! There is no such problem with exec.

After reading this, the article "what is the difference between xargs,-exec and pipes in shell" has been introduced. If you want to master the knowledge of this article, you still need to practice and use it to understand it. If you want to know more about related articles, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report