歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> 關於Linux >> Linux輸入輸出重定向

Linux輸入輸出重定向

日期:2017/3/1 14:46:44   编辑:關於Linux
Linux輸入輸出重定向 輸入輸出重定向,0-STDIN,1-STDOUT,2-STDERR ls -al test1 test2 test3 2>error 1>normal 可以同時輸出ls -al test1 test2 test3 &> all 3. 臨時重定向gr.sh: echo ‘hello’ >&2;echo ‘world’ 執行./gr.sh 2> error.txt,則會把錯誤結果輸出到error.txt而world則會輸出到屏幕上 4. 永久重定向:exec exec 1>test.txt則下面的echo語句輸出都會指向test.txt,一旦重定向,就無法輕易重定向加原來的位置,後面進行講解 輸入重定向:exec 0< test.txt,將會從test.file中讀取 While read line Do Echo $line Done 創建自己永久的重定向 (大於等於3) Exec 3> grself.txt Echo ‘hello world’ >&3 5. 重定向文件描述符 Exec 3>&1 #把3定向到1 Exec 1>test.txt #把1定向到test.txt文件 Echo ‘helloworld’ >&1 Exec 1>&3#把1定向到3,即還原到最初狀態 6. 輸入文件描述符 Exec 6<&0#把0定向到6 Exec 0<testfile #把testfile定向到0 Read line#從testfile中讀取 Exec 0<&6#把6定向到0 7. 創建讀寫文件描述符 既可以讀又可以寫 Exec 3<>testfile Read line <&3 Echo $line Echo ‘hello world’ >&3 8. 關閉文件描述符 Exec 3>&- 列出打開的文件描述符 /usr/bin/lsof -a -p $$ -d 0,1,2 9. 阻止命令輸出 echo ‘hello’>/dev/null 10. 創建臨時文件 Mktemp testing.XXXXX(至少三個X,系統隨機生成,保證生成的臨時文件惟一),返回創建後臨時文件名 Mktemp -t test.XXXXXX (-t 選項會強制在系統的/tmp下創建臨時文件,並會返回臨時文件的全路徑,而不止是文件名) 創建臨時目錄 tempdir=`mktemp -d dir.XXXXXX` cd $tempdir tempfile1=`mktemp temp.XXXXXXX` tempfile2=`mktemp temp.XXXXXXX` 11. 記錄信息:tee 向屏幕輸出同時寫入文件 Ls -al | tee test.txt Ls -al | tee -a test.txt (-a選項,表示在文件中追加)
Copyright © Linux教程網 All Rights Reserved