目錄
stream(資料流)是什麼意思
Linux資料流重定向
首頁 運維 linux運維 linux中stream什麼意思

linux中stream什麼意思

Mar 17, 2023 am 09:55 AM
linux stream

<blockquote><p>在linux中,stream是資料流的意思,就是以一定順序讀取的一串數據,所以資料流的方向就是資料流的讀取順序。 Linux系統把資料讀取後輸出的結果導入到其他檔案的過程稱為重定向資料流。 Linux下輸入一段命令並運行以後,螢幕裡會顯示兩種結果:運行成功結果即標準輸出、運行失敗結果即標準錯誤輸出;如果不做處理,它們都會顯示在螢幕上,而透過資料流重定向就可將其儲存到其他的文件中。 </p></blockquote> <p><img src="/static/imghw/default1.png" data-src="https://img.php.cn/upload/article/202303/17/2023031709544952034.jpg" class="lazy" alt="linux中stream什麼意思" ></p> <p>本教學操作環境:linux7.3系統、Dell G3電腦。 </p> <p><strong>在linux中,stream就是資料流的意思。 </strong></p> <h2 id="strong-stream-資料流-是什麼意思-strong"><strong>stream(資料流)是什麼意思</strong></h2> <p>stream(資料流)就是一個以一定順序讀取的一串數據,所以資料流的方向就是資料流的讀取順序。 </p> <p>資料流最初是通訊領域使用的概念,代表傳輸中所使用的資訊的數位編碼訊號序列。這個概念最初在1998年由Henzinger在文獻87中提出,他將資料流定義為「只能以事先規定好的順序被讀取一次的資料的一個序列」。 </p> <p>Linux系統重定向資料流就是把資料讀取後輸出的結果匯入到其他檔案中。 </p> <h2 id="strong-Linux資料流重定向-strong"><strong>Linux資料流重定向</strong></h2> <p>資料流重定向簡單來說就是將某條指令的輸入資料來源或輸出資料定位到其他的檔案。 Linux下一切皆文件。這裡的文件可以是設備文件、普通文件等。 </p> <p><a id="1_stdin_stdout_stderr_5"></a><span style="font-size: 18px;"><strong>1、stdin stdout stderr</strong></span></p> <blockquote><p><code>stdin</code>:標準輸入。指的是一些為命令運行所提供的資訊。 <br><code>stdout</code>:標準輸出。指的是命令執行所傳回的正確訊息。 <br><code>stderr</code>:標準錯誤輸出。指的是命令執行失敗後所傳回的錯誤訊息。 </p></blockquote> <p>這三者與指令的關係通常如下:</p> <div class="mermaid sequence-diagram"><svg id="mermaid-svg-43Q0mtxvjga7WpCa" width="100%" xmlns="http://www.w3.org/2000/svg" height="158" style="max-width: 436.46875px;" viewbox="0 0 436.46875 158"><style>#mermaid-svg-43Q0mtxvjga7WpCa {font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-serif;font-size:16px;fill:#333;}#mermaid-serif 43Q0mtxvjga7WpCa .error-icon{fill:#552222;}#mermaid-svg-43Q0mtxvjga7WpCa .error-text{fill:#552222;lines:#5522222;error-text{fill:#552222;lines:#552222; :2px;}#mermaid-svg-43Q0mtxvjga7WpCa .edge-thickness-thick{描邊-width:3.5px;}#mermaid-svg-43Q0mtxvjga7WpCa .edge-pattern-solid{0-das-solrayid svg- 43Q0mtxvjga7WpCa .edge-pattern-dashed{行程-dasharray:3;}#mermaid-svg-43Q0mtxvjga7WpCa .edge-pattern-dotted{行程-dasharray:2;}#mermaid-smtm-43357p. ;行程:#333333;}#mermaid-svg-43Q0mtxvjga7WpCa .marker.cross{行程:#333333;}#mermaid-svg-43Q0mtxvjga7WpCa svg{字體系列:"trebuchet msal,ver. :16px;}#mermaid-svg-43Q0mtxvjga7WpCa .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-43Q0mtxvjga-Wplab #333;}#mermaid-svg-43Q0mtxvjga7WpCa .cluster-label span{color:#333;}#mermaid-svg-43Q0mtxvjga7WpCa .label 文本,#mermaid-svg-43703; }#mermaid-svg-43Q0mtxvjga7WpCa .node 矩形,#mermaid-svg-43Q0mtxvjga7WpCa .node 圓,#mermaid-svg-43Q0mtxvjga7WpCa .node sgmerm3m. vg-43 Q0mtxvjga7WpCa .node路徑{填色:#ECECFF;描邊:#9370DB;描邊寬度:1px;}#mermaid-svg-43Q0mtxvjga7WpCa .node .label{文字對齊:中心;}#mermaid-svg-43Q0mtxvjjjga7Wpable.node.指針;}#mermaid-svg-43Q0mtxvjga7WpCa .arrowheadPath{fill:#333333;}#mermaid-svg-43Q0mtxvjga7WpCa .edgePath .path{描邊:#3333333;20:2020cm. flowchart-連結{描邊:#333333;填入:無;}#mermaid-svg-43Q0mtxvjga7WpCa .edgeLabel{背景顏色:#e8e8e8;text-align:center;}#mermaid-svg-43Q0mtxv.aWhope:center;}#mermaid-svg-43Q0mtxv.S7Wp^p 0.5;背景-color:#e8e8e8;fill:#e8e8e8;}#mermaid-svg-43Q0mtxvjga7WpCa .cluster 矩形{fill:#ffffde;行程:#aaaa33;行程寬度:1px;}#mermaid-susterg-4337px14357px; {fill :#333;}#mermaid-svg-43Q0mtxvjga7WpCa .cluster span{顏色:#333;}#mermaid-svg-43Q0mtxvjga7WpCa div.mermaidTooltip{位置:絕對; ;字體系列:"trebuchet ms",verdana,arial,sans-serif;字體大小:12px;背景:hsl(80, 100%, 96.2745098039%);邊框:1pxsolid #aaaa33;邊框半徑:2px; ;z-索引:100;}#mermaid-svg-43Q0mtxvjga7WpCa :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;}</style> <g><g class="output"><g class="clusters"></g><g class="edgePaths"><g class="edgePath LS-A LE-B" style="opacity: 1;" id="L-A-B"><path class="path" d="M54.40625,79L61.493489583333336,79C68.58072916666667,79,82.75520833333333,79,96.9296875,79C111.10416666666667,79,125.27864583333333,79,132.36588541666666,79L139.453125,79" marker-end="url(#arrowhead34)" style="fill:none"></path><defs><marker id="arrowhead34" viewbox="0 0 10 10" refx="9" refy="5" markerunits="strokeWidth" markerwidth="8" markerheight="6" orient="auto"><path d="M 0 0 L 10 5 L 0 10 z" class="arrowheadPath" style="stroke-width: 1; stroke-dasharray: 1, 0;"></path></marker></defs></g><g class="edgePath LS-B LE-C" style="opacity: 1;" id="L-B-C"><path class="path" d="M229.451171875,56L237.5322265625,51.833333333333336C245.61328125,47.666666666666664,261.775390625,39.333333333333336,277.8069661458333,35.166666666666664C293.8385416666667,31,309.7395833333333,31,317.6901041666667,31L325.640625,31" marker-end="url(#arrowhead35)" style="fill:none"></path><defs><marker id="arrowhead35" viewbox="0 0 10 10" refx="9" refy="5" markerunits="strokeWidth" markerwidth="8" markerheight="6" orient="auto"><path d="M 0 0 L 10 5 L 0 10 z" class="arrowheadPath" style="stroke-width: 1; stroke-dasharray: 1, 0;"></path></marker></defs></g><g class="edgePath LS-B LE-D" style="opacity: 1;" id="L-B-D"><path class="path" d="M229.451171875,102L237.5322265625,106.16666666666667C245.61328125,110.33333333333333,261.775390625,118.66666666666667,277.8069661458333,122.83333333333333C293.8385416666667,127,309.7395833333333,127,317.6901041666667,127L325.640625,127" marker-end="url(#arrowhead36)" style="fill:none"></path><defs><marker id="arrowhead36" viewbox="0 0 10 10" refx="9" refy="5" markerunits="strokeWidth" markerwidth="8" markerheight="6" orient="auto"><path d="M 0 0 L 10 5 L 0 10 z" class="arrowheadPath" style="stroke-width: 1; stroke-dasharray: 1, 0;"></path></marker></defs></g></g><g class="edgeLabels"><g class="edgeLabel" style="opacity: 1;" transform="translate(96.9296875,79)"><g transform="translate(-17.5234375,-13)" class="label"><rect rx="0" ry="0" width="35.046875" height="26"></rect><foreignobject width="35.046875" height="26"><div style="display: inline-block; white-space: nowrap;"><span id="L-L-A-B" class="edgeLabel L-LS-A' L-LE-B"></span></div></foreignobject></g></g><g class="edgeLabel" style="opacity: 1;" transform="translate(277.9375,31)"><g transform="translate(-21.453125,-13)" class="label">#stdin<rect rx="0" ry="0" width="42.90625" height="26"> </rect><foreignobject width="42.90625" height="26"><div style="display: inline-block; white-space: nowrap;"><span id="L-L-B-C" class="edgeLabel L-LS-B' L-LE-C"></span></div></foreignobject></g></g><g class="edgeLabel" style="opacity: 1;" transform="translate(277.9375,127)">##stderr<g transform="translate(-22.703125,-13)" class="label"> <rect rx="0" ry="0" width="45.40625" height="26"></rect><foreignobject width="45.40625" height="26"><div style="display: inline-block; white-space: nowrap;"><span id="L-L-B-D" class="edgeLabel L-LS-B' L-LE-D"></span></div></foreignobject></g></g>##stdout</g> ########### ################ <g class="nodes"><g class="node default" style="opacity: 1;" id="flowchart-A-18" transform="translate(31.203125,79)"><rect rx="5" ry="5" x="-23.203125" y="-23" width="46.40625" height="46" class="label-container"></rect>畫面/檔案<g class="label" transform="translate(0,0)"><g transform="translate(-13.203125,-13)"><foreignobject width="26.40625" height="26"><div style="display: inline-block; white-space: nowrap;"></div></foreignobject></g></g></g><g class="node default" style="opacity: 1;" id="flowchart-B-19" transform="translate(184.84375,79)"><rect rx="5" ry="5" x="-45.390625" y="-23" width="90.78125" height="46" class="label-container"></rect> #螢幕/檔案<g class="label" transform="translate(0,0)"></g></g></g></g></g></svg></div> <p>在Linux環境下,我們輸入一段指令並且運行以後,螢幕裡會顯示兩個結果:運行成功結果即標準輸出、運行失敗結果即標準錯誤輸出。 </p> <ul style="list-style-type: disc;"> <li><p>標準輸出(STDOUT): Standard output,指的是指令執行所傳回的正確訊息。 </p></li> <li><p>標準錯誤輸出(STDERR): Standard error output,指的是指令執行所傳回的錯誤訊息。 </p></li> </ul> <p>如果不對STDOUT和STDERR做處理,他們都會顯示在螢幕上,透過資料流重定向就可以將其儲存到其他的檔案中,而將其儲存在檔案中的特殊字元有:</p> <ul style="list-style-type: disc;"> <li><p>標準輸入stin:程式碼為0,使用<code><</code>或<code><<</code></p></li><li>##標準輸出stout:程式碼為1,使用<p>><code>或</code>>><code></code></code></p></li> <li>標準錯誤輸出stderr:程式碼為2,使用<p>2><code>或</code>2>><code></code></p> </li> </ul> <p><a id="2__21"></a><span style="font-size: 18px;"><strong></strong></span></p> <ul><li><code></code></li></ul> <p><code></code><code></code></p> <ul><li><code></code></li></ul> <p><code></code><code></code></p> <ul>## 2.資料流重定向的使用<li><code></code></li> </ul> <p>stdout<code>:標準輸出重定向。這裡重定向的是正確輸出。 </code><code><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">find / -name '.bashrc' > ~/out_right</pre><div class="contentsignin">登入後複製</div></div></code>此指令將正確輸出定向輸入到檔案<code>~/out_right</code>。如果<code>~/out_right</code>不存在,則自動建立。因為沒對錯誤訊息處理,錯誤輸出預設輸出到螢幕終端機上,此時終端機只會顯示錯誤輸出。 <code></code><code></code>stderr<code>:標準錯誤輸出重定向。這裡重定向的是錯誤輸出。 </code></p> <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">find / -name '.bashrc' 2> ~/out_error</pre><div class="contentsignin">登入後複製</div></div> <p>此指令將錯誤輸出定向輸入到檔案<a id="3_2_47">~/out_error</a>。如果<span style="font-size: 18px;">~/out_error<strong>不存在,則自動建立。因為沒有對正確資訊處理,正確輸出預設輸出到螢幕終端機上,此時終端機只會顯示正確輸出(如果沒找到指定文件,螢幕沒有輸出)。 </strong><code><strong></strong>stdin</code>:一般使用標準輸入來取代鍵盤輸入。 </span></p> <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">cat > catfile < ~/.bashrc</pre><div class="contentsignin">登入後複製</div></div><ul><li>cat<code>指令後不接任何參數,cat會從鍵盤讀取資料。使用標準輸入,將輸入來源定向到檔案</code>~/.bashrc<strong>。然後透過</strong>><code>將標準輸出定位到</code>catfile<code>檔案。 </code>cat<code>是檢視檔案的指令,這裡直接讀取檔案</code>~/.bashrc<code>然後將其輸出到</code>catfile<code>。 </code><code></code><code></code><code>3、</code><code></code>>>,2>>,<<</li><li><code></code><strong></strong><code>#>>(累加</pre> <li>):附加標準輸出。 <code>cmd > file1</code>,命令<strong>cmd</strong>將<code>stdout</code>輸出到</li>file1中,如果<p>fiel1<a id="4_stdoutstderr_62">先前存在,且有數據,那麼</a>file1<span style="font-size: 18px;">會事先被清空,然後寫入<strong>stdout</strong>。而使用指令<strong>cmd >> file1<code>,不會預先清空</code>file1<code>文件,而是直接在文件末端加上輸入的資料。 </code></strong></span>2>></p>(<p>累加<code>):附加標準錯誤輸出。與</code>>><code>相似。 </code></p> <p><<</p>(<blockquote>指定結束關鍵字<p>):後面接著<code>結束的輸入字元</code>。關鍵字必須獨佔一行,否則無效。 <code></code>34ebb5afdc0ee0107b681ebf4ee4b472###實作方法不只一種,我們取較好理解的寫法###find / -name .bashrc > list 2>&1### 。這段程式碼中的###2>&1###可以理解為,將標準錯誤輸出(代碼為2)重定向為標準輸出(代碼為1)。然後###> list###將標準輸出重定向到檔案###list###。因為標準錯誤輸出被定向為標準輸出,所以最後所有輸出都被導向到檔案###list###。 ######相關推薦:《###Linux影片教學###》###</p>

以上是linux中stream什麼意思的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

<🎜>:泡泡膠模擬器無窮大 - 如何獲取和使用皇家鑰匙
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
北端:融合系統,解釋
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
Mandragora:巫婆樹的耳語 - 如何解鎖抓鉤
3 週前 By 尊渡假赌尊渡假赌尊渡假赌

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

熱門話題

Java教學
1665
14
CakePHP 教程
1424
52
Laravel 教程
1322
25
PHP教程
1270
29
C# 教程
1249
24
Linux體系結構:揭示5個基本組件 Linux體系結構:揭示5個基本組件 Apr 20, 2025 am 12:04 AM

Linux系統的五個基本組件是:1.內核,2.系統庫,3.系統實用程序,4.圖形用戶界面,5.應用程序。內核管理硬件資源,系統庫提供預編譯函數,系統實用程序用於系統管理,GUI提供可視化交互,應用程序利用這些組件實現功能。

git怎麼查看倉庫地址 git怎麼查看倉庫地址 Apr 17, 2025 pm 01:54 PM

要查看 Git 倉庫地址,請執行以下步驟:1. 打開命令行並導航到倉庫目錄;2. 運行 "git remote -v" 命令;3. 查看輸出中的倉庫名稱及其相應的地址。

vscode上一步下一步快捷鍵 vscode上一步下一步快捷鍵 Apr 15, 2025 pm 10:51 PM

VS Code 一步/下一步快捷鍵的使用方法:一步(向後):Windows/Linux:Ctrl ←;macOS:Cmd ←下一步(向前):Windows/Linux:Ctrl →;macOS:Cmd →

notepad怎麼運行java代碼 notepad怎麼運行java代碼 Apr 16, 2025 pm 07:39 PM

雖然 Notepad 無法直接運行 Java 代碼,但可以通過借助其他工具實現:使用命令行編譯器 (javac) 編譯代碼,生成字節碼文件 (filename.class)。使用 Java 解釋器 (java) 解釋字節碼,執行代碼並輸出結果。

sublime寫好代碼後如何運行 sublime寫好代碼後如何運行 Apr 16, 2025 am 08:51 AM

在 Sublime 中運行代碼的方法有六種:通過熱鍵、菜單、構建系統、命令行、設置默認構建系統和自定義構建命令,並可通過右鍵單擊項目/文件運行單個文件/項目,構建系統可用性取決於 Sublime Text 的安裝情況。

Linux的主要目的是什麼? Linux的主要目的是什麼? Apr 16, 2025 am 12:19 AM

Linux的主要用途包括:1.服務器操作系統,2.嵌入式系統,3.桌面操作系統,4.開發和測試環境。 Linux在這些領域表現出色,提供了穩定性、安全性和高效的開發工具。

laravel安裝代碼 laravel安裝代碼 Apr 18, 2025 pm 12:30 PM

要安裝 Laravel,需依序進行以下步驟:安裝 Composer(適用於 macOS/Linux 和 Windows)安裝 Laravel 安裝器創建新項目啟動服務訪問應用程序(網址:http://127.0.0.1:8000)設置數據庫連接(如果需要)

git軟件安裝 git軟件安裝 Apr 17, 2025 am 11:57 AM

安裝 Git 軟件包括以下步驟:下載安裝包運行安裝包驗證安裝配置 Git安裝 Git Bash(僅限 Windows)

See all articles