23 shell replacement process

  • 0.shell process substitution usage
  • 1. Necessity of using the process to replace
  • 2. Replace the nature of the process

And command substitution process is very similar. Alternatively command is the output of a command assigned to another variable , for example, dir_files=`ls -l`or date_time=$(date); the process alternative is to pass the output of a command to another (group) command .

0.shell process substitution usage

Writing meaning important point Nature
<(commands)

It means of the input redirection, its output may be used as another command input

command list is a set of commands to command a plurality semicolon ;separated.

Note that, <or >there is no space between the parentheses .

/ Dev / fd / n document acceptance (Commands) output, as another command input
>(commands)

It means of output redirection, to accept another command output standard

Reading from / dev / fd / n file as (Commands) input

1. Necessity of using the process to replace

For Li
result
Explanation

Normal mode

  1. echo "aaaaa" read AA
  2. echo $AA
The output is empty

Shell command in the parent echo, whereas in the read command in the sub-Shell ,

When the read execution sub Shell is destroyed, AA variable disappears.

Pipeline command is always executed in the sub-Shell, any command to assign values ​​to variables will be met with this problem.

Use process substitution

  1. read AA<(echo "aaaaa")
  2. echo $AA
The output is aaaaa

On the whole, Shell the echo "aaaaa"output result is read as an input.

<()Used to capture the output of the echo command, <to redirect the result to read.

Note that two <is a space between the first <represents the input redirection, second <and ()together represent the replacement process.

read command and the second echo commands run in the current process of Shell ,

Read data is saved to the current process variable AA, it is possible to successfully use echo output.

Examples of processes used to replace "accepts standard input" in

echo "qpy" > >(read; echo "你好,$REPLY")

Run Results: Hello, qpy Because of the use of the redirection, read commands from echo "qpy"the output of the read data.

2. Replace the nature of the process

To be able to transfer data between different processes, in fact, the process of replacement will associate with the system files , the name of this file is /dev/fd/n(n is an integer). The file is passed as an argument to ()the command, ()the command is to read or write to the file depends on the format of the replacement process is <still >:

  • If so >(), then the file will give ()the command to provide input; With output redirection, what to enter may come from other commands.
  • If yes <(), then the file receives ()the output of the command; via the input redirection, the contents of the file can be used as input other commands.


Use the echo command to view the process of replacing the corresponding file name:

command Export Explanation

echo >(true)

/dev/fd/63

/dev/fd/There are a lot of numbers in the directory file, the process is generally used to replace 63 file, which is an internal file system, we generally see less.

/dev/fd/63Played a role in data transfer files or data bridge, with redirection, it will >()internal commands and external commands linked

So that data can flow between these commands.

echo <(true)

/dev/fd/63

echo >(true) <(true)

/dev/fd/63 /dev/fd/62

echo "qpy" > >(read; echo "hello, $REPLY")

hello, qpy

The first >represents the output redirection, which outputs the result of the first echo command is redirected to /dev/fd/63a file.

>()The read command from the /dev/fd/63中读取内容reading, and then outputs a read command to the echo of the content.

 

Guess you like

Origin www.cnblogs.com/mianbaoshu/p/12069788.html