国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 操作系統 > 正文

深入理解 bash redirection 重定向

2024-06-28 13:21:27
字體:
來源:轉載
供稿:網友
深入理解 bash redirection 重定向stdout redirection
?  ganiks@bash-redirection  cat 123 111111112222222233333333?  ganiks@bash-redirection  cat 123 >> abc ?  ganiks@bash-redirection  echo "one line" >> abc#當cat不帶參數的時候,表示使用標準輸入作為輸入,這允許在標準輸入中鍵入相關的內容?  ganiks@bash-redirection  cat >> abc444444445555555566666666^C    # ctrl+C?  ganiks@bash-redirection  cat abc 111111112222222233333333one line444444445555555566666666

參考文檔

stderr to stdout
?  ganiks@bash-redirection  cat > find.sh                                find . -name "*" -PRint -exec grep "555" {} /;^C?  ganiks@bash-redirection  sudo chmod +x find.sh?  ganiks@bash-redirection  ./find.sh .grep: .: 是一個目錄./abc55555555./nginx-V./find.shfind . -name "*" -print -exec grep "555" {} /;./123#第一種情況?  ganiks@bash-redirection  ./find.sh 2>&1 > find1.loggrep: .: 是一個目錄grep: 輸入文件 ‘./find1.log’ 同時也作輸出#第一種情況?  ganiks@bash-redirection  ./find.sh > find2.log 2>&1#結果1?  ganiks@bash-redirection  cat find1.log ../abc55555555./nginx-V./find.shfind . -name "*" -print -exec grep "555" {} /;./123./find1.log#結果2  ?  ganiks@bash-redirection  cat find2.log .grep: .: 是一個目錄./abc55555555./nginx-V./find.shfind . -name "*" -print -exec grep "555" {} /;./123./find1.log55555555find . -name "*" -print -exec grep "555" {} /;./find2.loggrep: 輸入文件 ‘./find2.log’ 同時也作輸出

0 是 stdin1 是 stdout2 是 stderr

? ./find.sh 2>&1 > find1.log? ./find.sh > find2.log 2>&1

這里分析的關鍵是:一步一步分析,分析一步,輸出一步

  • 第一種情況:

    • 2>&1: 將stderr定向到stdout, 原本stdout就是要到屏幕的,所以stderr會輸出到屏幕
    • >find1.log: 將stdout 再重定向到文件
  • 第二種情況:

    • >find2.log: 將stdout重定向到文件
    • 2>&1: 將stderr重定向到stdout, 此時stdout已經到文件了,自然stderr也要到文件

重定向的過程其實很簡單,但由于和直觀感受不一致,往往導致初學者在這里犯很多錯誤。 參考文檔

基本的IO重定向

基本IO重定向操作

  • > file: 將stdout重定向到file
  • < file: 將file作為stdin
  • cmd1 | cmd2 : pipe,將cmd1 的標準輸出作為cmd2 的標準輸入
  • >>file:將標準輸出重定向到file,如果file存在,append到文件中,即附加到文件的后面,而不是覆蓋文件
  • cat >> file: 當cat不帶參數的時候,表示使用標準輸入作為輸入,這允許在標準輸入中鍵入相關的內容
  • > | file: 強制將標準輸出重定向到file,即使noclobber設置。當設置環境變量set –o noclobber,將禁止重定向到一個已經存在的文件中,避免文件被覆蓋。
  • n >|file: 強制將文件描述符n重定向到file,即使noclobber打開
  • <>file: 將file作為標準輸入和標準輸出。通常用于設備文件(/dev下面的文件),底層系統程序員可用之測試設備驅動,其他的很少用
  • n <>file: 將file作為文件描述符n的輸入和輸出
  • << label: Here-document; : 將shell的標準輸入作為命令的輸入,直到行中之包含label。這中間的輸入成為here-document。
?  ganiks@bash-redirection  cat >> msgfile <<.heredoc> this is the text ofheredoc> our messageheredoc> end with .heredoc> .   #這里<<.表明以.為結束。因此無需使用^D,而改用. ?  ganiks@bash-redirection  cat msgfile this is the text ofour messageend with .
  • n>file :將文件描述符n重定向到file
  • n :將file作為文件描述符的輸入
  • n>>file :將文件描述符n的輸出重定向到file,如果file存在,將輸出append到文件后面
  • n>& :將標準輸出復制到文件描述符n(Duplicate standard output to file descriptor n)
  • n<& :從文件描述符n復制標準輸入(Duplicate standard input from file descriptor n)
  • n>&m :文件描述字n將一個copy至文件描述字m(File descriptor n is made to be a copy of the output file descriptor)
  • n<&m :文件描述字n作為文件描述字m中的一個拷貝(File descriptor n is made to be a copy of the input file descriptor)
  • &>file : 將標準輸出和標準錯誤輸出定向至文件file
  • <&- : 關閉標準輸入
  • >&- : 關閉標準輸出
  • n>&- : 關閉文件描述字作為輸出(Close the output from file descriptor n)
  • n<&- :關閉文件描述字作輸入(Close the input from file descriptor n)
  • n>&Word: If n is not specified, the standard output (file descriptor 1) is used. If the digits in word do not specify a file descriptor open for output, a redirection error occurs. As a special case, if n is omitted, and word does not expand to one or more digits, the standard output and standard error are redirected as described previously.
  • n<&word : If word expands to one or more digits, the file descriptor denoted by n is made to be a copy of that file descriptor. If the digits in word do not specify a file descriptor open for input, a redirection error occurs. If word evaluates to -, file descriptor n is closed. If n is not specified, the standard input (file descriptor 0) is used.
  • n>&digit- : Moves the file descriptor digit to file descriptor n, or the standard output (file descriptor 1) if n is not specified.
  • n<&digit- : Moves the file descriptor digit to file descriptor n, or the standard input (file descriptor 0) if n is not specified. digit is closed after being duplicated to n.
文件描述符

文件描述符在bash中比較少用,從0開始用戶表示進行的數據流,0表示標準輸入,1表示標準輸出,2表示標注錯誤輸出,其他從3開始。最為常用的場景是將錯誤消息輸出到某個文件,可以加上2>file 到我們的命令中。  我們來看下面一個腳本的例子:

command > logfile 2>&1 &
  • >logfile 表示command的標準輸出重定向至文件logfile中
  • 2>&1,匹配n>&m,表示文件描述字2(command的標準錯誤輸出)將copy一份采用文件描述字1(即標準輸出),由于標準輸出已經重定向logfile,這份copy也見將重定向至文件lofgile。我們可以用“abcd > logfile 2>&1 &”來驗證這個效果。

下面可達到類似的效果:

command 2>&1 | tee logfile &

錯誤輸出同樣適用標準輸出,通過pipe方式,見他們作為輸入執行tee logfile。tee命令將它的標準輸入copy至他的標準標準輸出以及參數所帶的文件中。和上面的命令不一眼這里即會在stdout 和logfile中同時輸出。

其他文件描述字的重定向,例如<&n,通常用于從多個文件中讀入或者寫出。

  • <&- ,表示強制關閉標準輸入
  • >&- ,表示強制關閉標準輸出
  • 1> ,等同于>
  • 0< ,等同于<

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 钟山县| 建平县| 永宁县| 尚义县| 曲阜市| 宁乡县| 龙州县| 博客| 固始县| 镇宁| 宜黄县| 河津市| 盱眙县| 石首市| 绥阳县| 宁陵县| 临西县| 贵南县| 公安县| 汉阴县| 安图县| 湖北省| 安顺市| 张家口市| 德阳市| 伊宁县| 高雄县| 包头市| 静安区| 壶关县| 兴安盟| 个旧市| 綦江县| 双牌县| 佳木斯市| 民和| 莱州市| 甘肃省| 兴海县| 华阴市| 东港市|