溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

shell如何輸出重定向

發(fā)布時(shí)間:2022-03-23 14:31:49 來(lái)源:億速云 閱讀:171 作者:小新 欄目:web開(kāi)發(fā)

這篇文章主要為大家展示了“shell如何輸出重定向”,內(nèi)容簡(jiǎn)而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“shell如何輸出重定向”這篇文章吧。

輸出重定向

如果你愿你,可以將STDERR 和 STDOUT 的輸出重定向到一個(gè)輸出文件,為此,bash 提供了特殊的重定向符號(hào) &>

ls file nofile &> /dev/null


我們?nèi)绾卧谀_本里面重定向呢?沒(méi)有什么特別之處,和普通重定向一樣。

#!/bin/bash
#redirecting output to different locations
echo "now redirecting all output to another location" &>/dev/null


問(wèn)題就來(lái)了,如果我們要將所有的輸出都重定向到某個(gè)文件呢?我們都不希望每次輸出的時(shí)候都重定向一下吧,正所謂,山人自有妙計(jì)。我們可以用exec 來(lái)永久重定向,如下所示:

#!/bin/bash
#redirecting output to different locations
exec 2>testerror
echo "This is the start of the script"
echo "now redirecting all output to another location"

exec 1>testout
echo "This output should go to testout file"
echo "but this should go the the testerror file" >& 2


輸出結(jié)果如下所示:

This is the start of the script
now redirecting all output to another location
lalor@lalor:~/temp$ cat testout
This output should go to testout file
lalor@lalor:~/temp$ cat testerror
but this should go the the testerror file
lalor@lalor:~/temp$


以追加的方式重定向:

exec 3 >> testout


取消重定向:

exec 3> -

shell是什么

Shell 是一個(gè)用 C 語(yǔ)言編寫的程序,它是用戶使用 Linux 的橋梁。Shell 既是一種命令語(yǔ)言,又是一種程序設(shè)計(jì)語(yǔ)言。Shell還是指一種應(yīng)用程序,這個(gè)應(yīng)用程序提供了一個(gè)界面,用戶通過(guò)這個(gè)界面訪問(wèn)操作系統(tǒng)內(nèi)核的服務(wù)。

以上是“shell如何輸出重定向”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI