Mkfifo Use of Linux-shellscript

Source: Internet
Author: User


A "FIFO" is a special type of file that allows independent processes to communicate. A process opens a FIFO file for write operations, and another process reads it. The data can then be run in the same way as anonymous pipelines that are common in the shell or elsewhere.
Options (optional)

-M mode,--mode=mode
Set the mode of the FIFO that is created to mode, which can be a symbol pattern in chmod (1) and use the default mode as the starting point.

--help
Prints a usage message on the standard output and exits in a successful state.
--version

Prints the version information on the standard output and exits in a successful state.
--
< Span style= "text-decoration:underline; font-size:14px; " >
    termination Options list.
    a
The following example illustrates the use of the command:

 [[email protected] ~]# mkfifo  fifo //Create a FIFO 
[[Email protected] ~]# echo example >example.txt  
[[email protected] ~]# cat example.txt 
Example
[[email  protected] ~]# cat example.txt > fifo //in the background convenient
^c
[[email  Protected] ~]# cat example.txt > fifo &
[1] 2002
[[email  protected] ~]# cat < fifo //fifo  Import  cat
example            //successfully exported the data in Example.txt
[1]+  done                      cat example.txt > fifo  
[[email protected] ~]# cat <  fifo



Personal understanding: FIFO is equivalent to the pipeline, he is only the load data-driven transmission, is not responsible for storing data, so we first cat < FIFO display data, the second time is gone, like the water will not stay in the source.

[[email protected] ~]# ll FIFO
prw-r--r--1 root root 0 8 12:56 FIFO

Size of 0 is sufficient for the card


Next is a shell program that uses FIFO to distribute the primary-to-slave task

& nbsp

#########################################################################

2 # File Name:master.sh
3 # Author:dty
4 # Mail: [email protected]
5 # Created Time:fri 09:04:53 PM CST
6 #########################################################################
7 #!/bin/bash
8 pid=$$//Get PID of the current script
9 Fifo=/tmp/fifo. $pid//Set PID unique FIFO name, and path
Log=/tmp/log. $pid//Record operation
> $log//Empty files to ensure purity
echo "My PID is $pid"//output PID, easy to use from the following
Mkfifo $fifo//Set up FIFO!
While:
The
Echo-en "Give me a command for one of my minions:"
+ Read cmd
echo $cmd > $fifo//Import what you have entered into the FIFO
Done
RM-FR $log $fifo//End of cycle delete FIFO and record

#########################################################################

2. From

2 # File Name:minion.sh
3 # Author:dty
4 # Mail: [email protected]
5 # Created Time:sat 12:06:30 PM CST
6 #########################################################################
7 #!/bin/bash
8 master=$1//$1 need us to manually enter the displayed PID as set in the main script.
9 Fifo=/tmp/fifo. $master//FIFO of the Master
Log=/tmp/log. $master//The master record
11
While:
Do
Read cmd args < $FIFO//import the master FIFO into read, at which time the variable cmd args is what you entered on Master
if [!-Z "$cmd"];then//
If ["$cmd" = = "quit"];then//If you enter quit
echo "very good,master." | Tee-a $log//exit while adding very good,master to the record, indicating that a script has exited from the
Exit 0//Quit
Fi
echo "' Date ': Executing \" ${cmd}\ "for the master." |tee-a $log//Display the command you entered
if [!-Z "$args"];then//If your command has a second parameter above
For Arg in $args
% do
Echo-en "$args" |tee-a $log//Output The second parameter and append to the record
Sleep 1
+ Done

echo | Tee-a $log//output a blank line to record, easy to identify
-Fi
Fi
Sleep 10
Done
~

Run the script from the input main PID to establish the connection, enter the command, the upper right corner of the main, the lower right corner of the record, the left is from the script.

As follows











It can be seen that the FIFO master is equivalent to one end of the pipeline, from the other end, thus ensuring that the data is transferred between different scripts ~ ~




Mkfifo Use of Linux-shellscript

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.