国产精品白浆熟女,国产偷亚洲偷欧美偷精品,,新免费无码国产在线看,国产激情久久久久影院老熟女

Linux I/O重定向

JSON 2024-05-17 16:13:56 6705

Unix 命令行的功能之一是使用輸入/輸出重定向和管道。在本次會(huì)議中,我們介紹了輸入、輸出和錯(cuò)誤流的重定向。

Linux I/O重定向

標(biāo)準(zhǔn)輸入、標(biāo)準(zhǔn)輸出和標(biāo)準(zhǔn)錯(cuò)誤

bash shell 具有三個(gè)基本流;它從 stdin(流 0)獲取輸入,將輸出發(fā)送到 stdout(流 1),并將錯(cuò)誤消息發(fā)送到 stderr(流 2)。

鍵盤通常用作標(biāo)準(zhǔn)輸入,而標(biāo)準(zhǔn)輸出和標(biāo)準(zhǔn)錯(cuò)誤都連接到顯示器。這可能會(huì)讓 Linux 新用戶感到困惑,因?yàn)闆]有明顯的方法來識別 stdout 和 stderr。有經(jīng)驗(yàn)的用戶知道將輸出與錯(cuò)誤分開非常有用。

下一節(jié)將解釋如何重定向這些流。

輸出重定向

標(biāo)準(zhǔn)輸出 (>)

stdout 可以使用大于號重定向。掃描該行時(shí),shell 將看到 > 符號并清除文件。

事實(shí)上,> 符號是 1> 的縮寫(stdout 被稱為流 1)。

datasoft @ datasoft-linux ~/test10$ echo It is summer today!
It is summer today!
 datasoft @ datasoft-linux ~/test10$ echo It is summer today! > summer.txt
 datasoft @ datasoft-linux ~/test10$ cat summer.txt
It is summer today!
 datasoft @ datasoft-linux ~/test10$ 

請注意,bash shell 在執(zhí)行參數(shù) 0 之前有效地從命令行刪除了重定向。這意味著在此命令的情況下:

datasoft @ datasoft-linux ~/test10$ echo Hei > greeting.txt

shell 只計(jì)算兩個(gè)參數(shù)(echo = 參數(shù) 0,hello = 參數(shù) 1)。在參數(shù)計(jì)數(shù)發(fā)生之前重定向被刪除。

輸出文件被刪除

掃描該行時(shí),shell 將看到 > 符號并清除文件!由于這發(fā)生在解析參數(shù) 0 之前,這意味著即使命令失敗,文件也將被清除!

datasoft @ datasoft-linux ~/test10$ cat summer.txt
It is summer today!
 datasoft @ datasoft-linux ~/test10$ zcho It is summer today! > summer.txt
No command 'zcho' found, did you mean:
 Command 'echo' from package 'coreutils' (main)
zcho: command not found
 datasoft @ datasoft-linux ~/test10$ cat summer.txt
 datasoft @ datasoft-linux ~/test10$

noclobber

過設(shè)置 noclobber 選項(xiàng)可以防止使用 > 時(shí)擦除文件。

datasoft @ datasoft-linux ~/test10$ cat summer.txt
 datasoft @ datasoft-linux ~/test10$ set -o noclobber
 datasoft @ datasoft-linux ~/test10$ echo It is cold today! > summer.txt
bash: summer.txt: cannot overwrite existing file
 datasoft @ datasoft-linux ~/test10$ set +o noclobber
 datasoft @ datasoft-linux ~/test10$ 

否決noclobber

noclobber 可以用 >| 來否決。

datasoft @ datasoft-linux ~/test10$ set -o noclobber
 datasoft @ datasoft-linux ~/test10$ echo It is summer today! > summer.txt
bash: summer.txt: cannot overwrite existing file
 datasoft @ datasoft-linux ~/test10$ echo It is summer today! >|summer.txt
 datasoft @ datasoft-linux ~/test10$ cat summer.txt
It is summer today!
 datasoft @ datasoft-linux ~/test10$

追加 (>>)

使用 >> 將輸出附加到文件。

datasoft @ datasoft-linux ~/test10$ echo It is summer today! > summer.txt
bash: summer.txt: cannot overwrite existing file
 datasoft @ datasoft-linux ~/test10$ cat summer.txt
It is summer today!
 datasoft @ datasoft-linux ~/test10$ echo Where is the hot summer ? >> summer.txt
 datasoft @ datasoft-linux ~/test10$ cat summer.txt
It is summer today!
Where is the hot summer ?
 datasoft @ datasoft-linux ~/test10$

錯(cuò)誤重定向

2> 標(biāo)準(zhǔn)錯(cuò)誤

重定向 stderr 是通過 2> 完成的。這對于防止屏幕上出現(xiàn)錯(cuò)誤消息非常有用。

下面的屏幕截圖顯示了 stdout 重定向到文件,stderr 重定向到 /dev/null。寫 1> 與 > 相同。

 datasoft @ datasoft-linux ~/test10$ find / > allfiles.txt 2> /dev/null
 datasoft @ datasoft-linux ~/test10$

2>&1

要將 stdout 和 stderr 重定向到同一文件,請使用 2>&1。

datasoft @ datasoft-linux ~/test10$ find / > allfiles_and_error.txt 2>&1

請注意,重定向的順序很重要。例如,命令

ls > dirlist 2>&1

將標(biāo)準(zhǔn)輸出(文件描述符 1)和標(biāo)準(zhǔn)錯(cuò)誤(文件描述符 2)定向到文件目錄列表,而命令

僅將標(biāo)準(zhǔn)輸出定向到文件 dirlist,因?yàn)闃?biāo)準(zhǔn)錯(cuò)誤在標(biāo)準(zhǔn)輸出重定向到 dirlist 之前復(fù)制了標(biāo)準(zhǔn)輸出。

輸出重定向和管道

默認(rèn)情況下,在命令行上使用管道時(shí),無法在 stderr 內(nèi)進(jìn)行 grep,因?yàn)橹粋鬟f了 stdout。

datasoft @ datasoft-linux ~/test10$ rm file35 file=10 | grep test
rm: cannot remove 'file35': No such file or directory
rm: cannot remove 'file=10': No such file or directory
 datasoft @ datasoft-linux ~/test10$ rm file10 | grep.txt

使用 2>&1 您可以強(qiáng)制 stderr 轉(zhuǎn)到 stdout。這使得管道中的下一個(gè)命令能夠作用于兩個(gè)流。

datasoft @ datasoft-linux ~/test10$ rm file35 file10 file101 2>&1 1>&2 | grep file35
rm: cannot remove 'file35': No such file or directory
 datasoft @ datasoft-linux ~/test10$ 

您不能同時(shí)使用 1>&2 和 2>&1 來切換 stdout 和 stderr。

datasoft @ datasoft-linux /$ rm file35 file10 file101 2>&1 1>&2 | grep file35
rm: cannot remove 'file35': No such file or directory
 datasoft @ datasoft-linux /$ echo file35 2>&1 1>&2 | sed 's/file35/FILE35/'
FILE35

您需要第三個(gè)流來在管道符號之后切換 stdout 和 stderr。

datasoft @ datasoft-linux /$ echo file35 2>&1 1>&2 | sed 's/file35/FILE35/'
FILE35
 datasoft @ datasoft-linux /$ echo file35 3>&1 1>&2 2>&3 | sed 's/file35/FILE35/' file35
file35
sed: can't read file35: No such file or directory
 datasoft @ datasoft-linux /$

連接 stdout 和 stderr

&> 構(gòu)造會(huì)將 stdout 和 stderr 放入一個(gè)流中(到一個(gè)文件)。

datasoft @ datasoft-linux ~$ cd test10
 datasoft @ datasoft-linux ~/test10$ rm file35 &> out_and_err
 datasoft @ datasoft-linux ~/test10$ cat out_and_err
rm: cannot remove 'file35': No such file or directory
 datasoft @ datasoft-linux ~/test10$ echo file35 &> out_and_err
bash: out_and_err: cannot overwrite existing file
 datasoft @ datasoft-linux ~/test10$ 

輸入重定向

標(biāo)準(zhǔn)輸入(<)

重定向標(biāo)準(zhǔn)輸入是通過 < (0< 的簡寫)完成的。

datasoft @ datasoft-linux ~$ cat < mno.txt
What is your name?
My name is prasanta.
 datasoft @ datasoft-linux ~$ tr 'onetw' 'onezz' < mno.txt
Whaz is your name?
My name is prasanza.
 datasoft @ datasoft-linux ~$

here-is-document是一種追加輸入直到遇到特定序列(通常是EOF)的方法。 EOF 標(biāo)記可以按字面輸入,也可以使用 Ctrl-D 調(diào)用。

檢查下面的代碼

datasoft @ datasoft-linux ~$ cat < mno.txt
> What is your name?
> My name is prasanta.
> EOF
bash: mno.txt: cannot overwrite existing file
 datasoft @ datasoft-linux ~$ cat mno.txt
What is your name?
My name is prasanta.
 datasoft @ datasoft-linux ~$ cat < mno.txt
> What's your name?
> prasanta
> ^C
 datasoft @ datasoft-linux ~$ cat mno.txt
What is your name?
My name is prasanta.
 datasoft @ datasoft-linux ~$


版權(quán)所屬:SO JSON在線解析

原文地址:http://zijieyoumin.cn/blog/532.html

轉(zhuǎn)載時(shí)必須以鏈接形式注明原始出處及本聲明。

本文主題:

如果本文對你有幫助,那么請你贊助我,讓我更有激情的寫下去,幫助更多的人。

關(guān)于作者
一個(gè)低調(diào)而悶騷的男人。
相關(guān)文章
Java 獲取HTPP 301 / 302 重定向后的Url地址,301和302對SEO的影響
Java獲取重定向后的真實(shí)URL地址
Nginx 跳轉(zhuǎn)到www二級域名,域名重定向配置方法。
for循環(huán)的 i++ 和 ++i 的區(qū)別
Linux—文件樹
Linux—文件樹
Linux 安裝 Redis 詳細(xì)步驟講解
Linux Centos 使用 Redis service 啟動(dòng),Redis service 腳本編寫
阿里云Linux、代理Squid服務(wù)安裝,高匿、用戶/密碼校驗(yàn)配置
最新文章
計(jì)算機(jī)網(wǎng)絡(luò)的相關(guān)內(nèi)容 239
SOJSON V6 JavaScript 解密技巧與分析 5802
微信客服人工電話95068:如何快速解封微信賬號(2025最新指南) 11575
Java Http請求,HttpURLConnection HTTP請求丟失頭信息,Head信息丟失解決方案 5036
實(shí)用API合集分享:教你輕松獲取IP地址的API合集 8803
Linux I/O重定向 6705
Ruby 循環(huán) - while、for、until、break、redo 和 retry 3990
Node.js:全局對象 3581
如何使用終端檢查Linux上的內(nèi)存使用情況 3779
JavaScript對象詳細(xì)剖析 3252
最熱文章
免費(fèi)天氣API,天氣JSON API,不限次數(shù)獲取十五天的天氣預(yù)報(bào) 744432
最新MyEclipse8.5注冊碼,有效期到2020年 (已經(jīng)更新) 702904
蘋果電腦Mac怎么恢復(fù)出廠系統(tǒng)?蘋果系統(tǒng)怎么重裝系統(tǒng)? 678320
Jackson 時(shí)間格式化,時(shí)間注解 @JsonFormat 用法、時(shí)差問題說明 561904
我為什么要選擇RabbitMQ ,RabbitMQ簡介,各種MQ選型對比 511792
Elasticsearch教程(四) elasticsearch head 插件安裝和使用 483712
Jackson 美化輸出JSON,優(yōu)雅的輸出JSON數(shù)據(jù),格式化輸出JSON數(shù)據(jù)... ... 299492
Java 信任所有SSL證書,HTTPS請求拋錯(cuò),忽略證書請求完美解決 246598
Elasticsearch教程(一),全程直播(小白級別) 232033
227509
支付掃碼

所有贊助/開支都講公開明細(xì),用于網(wǎng)站維護(hù):贊助名單查看

查看我的收藏

正在加載... ...