linux 下的 mkfifo、exec 命令使用


MKFIFO
Section: User Commands (1)
Updated: 1998年11月
Index Return to Main Contents  
NAME(名稱)
mkfifo - 創建FIFO(命名管道)  
SYNOPSIS(總覽)
mkfifo [options] file...

POSIX options(選項): [-m mode]

GNU options(選項)(最短格式): [-m mode] [--help] [--version] [--]  
DESCRIPTION(描述)
mkfifo 使用指定的文件名創建FIFO(也稱為"命名管道").

"FIFO"是一種特殊的文件類型,它允許獨立的進程通訊. 一個進程打開FIFO文件進行寫操作,而另一個進程對之進行讀操作, 然后數據便可以如同在shell或者其它地方常見的的匿名管道一樣流線執行.

默認情況下,創建的FIFO的模式為0666('a+rw')減去umask中設置的位.  
OPTIONS(選項)

-m mode, --mode=mode
    設置創建的FIFO的模式為 mode, 這可以是 chmod(1) 中的符號模式,並使用默認模式作為起始點. 

 
GNU STANDARD OPTIONS(GNU標准選項)

--help
    在標准輸出上打印一條用法信息,並以成功狀態退出. 
--version
    在標准輸出上打印版本信息,然后以成功狀態退出. 
-- 
    終止選項列表. 


 

Linuxexec的用法總結

exec 相關:http://blog.csdn.net/cyberrusher/article/details/7253385

先總結一個表:

exec命令

作用

exec ls

shell中執行lsls結束后不返回原來的shell中了

exec <file< span="">

file中的內容作為exec的標准輸入

exec >file

file中的內容作為標准寫出

exec 3<file< span="">

file讀入到fd3

sort <&3

fd3中讀入的內容被分類

exec 4>file

將寫入fd4中的內容寫入file

ls >&4

Ls將不會有顯示,直接寫入fd4中了,即上面的file

exec 5<&4

創建fd4的拷貝fd5

exec 3<&-

關閉fd3

 

1. exec 執行程序

 雖然execsource都是在父進程中直接執行,但exec這個與source有很大的區別,source是執行shell腳本,而且執行后會返回以前的shell。而exec的執行不會返回以前的shell了,而是直接把以前登陸shell作為一個程序看待,在其上經行復制。

舉例說明:

root@localhost:~/test#exec ls

exp1  exp5  linux-2.6.27.54  ngis_post.sh  test  xen-3.0.1-install

 

 

root@localhost:~/test#exec >text

root@localhost:~/test#ls

root@localhost:~/test#pwd

root@localhost:~/test#echo "hello"

root@localhost:~/test#exec>/dev/tty

root@localhost:~/test#cat text 

exp1

exp5

linux-2.6.27.54

ngis_post.sh

test

text

xen-3.0.1-install

/root/test

hello

root@localhost:~/test#

Exec >text 是將當前shell的標准輸出都打開到text文件中

 

root@localhost:~/test#cat test

ls

Pwd

root@localhost:~/test#bash

root@localhost:~/test#exec <test< span="">

root@localhost:~/test#ls

exp1  exp5  linux-2.6.27.54  ngis_post.sh  test  text  xen-3.0.1-install

root@localhost:~/test#pwd

/root/test

root@localhost:~/test#

root@localhost:~/test#exit       #自動執行

 

2. exec的重定向

  先上我們進如/dev/fd/目錄下看一下:

root@localhost:~/test#cd /dev/fd

root@localhost:/dev/fd#ls

0  1  2  255

默認會有這四個項:0是標准輸入,默認是鍵盤。

1是標准輸出,默認是屏幕/dev/tty

2是標准錯誤,默認也是屏幕

255

當我們執行exec 3>test時:

root@localhost:/dev/fd#exec 3>/root/test/test 

root@localhost:/dev/fd#ls

0  1  2  255  3

root@localhost:/dev/fd#

看到了吧,多了個3,也就是又增加了一個設備,這里也可以體會下linux設備即文件的理念。這時候fd3就相當於一個管道了,重定向到fd3中的文件會被寫在test中。關閉這個重定向可以用exec 3>&-

root@localhost:/dev/fd#who >&3

root@localhost:/dev/fd#ls >&3

root@localhost:/dev/fd#exec 3>&-

root@localhost:/dev/fd#cat /root/test/te

test  text  

root@localhost:/dev/fd#cat /root/test/test 

root     tty1         2010-11-16 01:13

root     pts/0        2010-11-15 22:01 (192.168.0.1)

root     pts/2        2010-11-16 01:02 (192.168.0.1)

0

1

2

255

3

 

 

 

 

3. 應用舉例:

        exec 3<test< span="">

        while read -u 3 pkg

do

echo "$pkg"

done


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM