目的

掌握錯(cuò)誤輸出、標(biāo)準(zhǔn)輸出、標(biāo)準(zhǔn)輸入使用;重定向和管道的使用方法。

前提

linu系統(tǒng)中的三種I/O設(shè)備所代表的編號(hào)分別是:標(biāo)準(zhǔn)輸入(STDIN),文件描述符為0,默認(rèn)從鍵盤獲取輸入;標(biāo)準(zhǔn)輸出(STDOUT),文件描述符為1,默認(rèn)輸出到顯示屏;標(biāo)準(zhǔn)錯(cuò)誤(STDERR),文件描述符為2,默認(rèn)輸出到顯示屏。

I/O重定向就是為了改變默認(rèn)輸入、輸出的位置:

:表示標(biāo)準(zhǔn)輸出覆蓋重定向;

:表示標(biāo)準(zhǔn)輸出追加重定向;

2>:表示錯(cuò)誤輸出覆蓋重定向;

2>>:表示錯(cuò)誤輸出追加重定向;

&>:表示合并標(biāo)準(zhǔn)輸出和錯(cuò)誤輸出覆蓋重定向;

&>>:表示合并標(biāo)準(zhǔn)輸出和錯(cuò)誤輸出追加重定向;

2>&1:表示意義同&>即合并標(biāo)準(zhǔn)輸出和錯(cuò)誤輸出覆蓋重定向;

<:輸入重定向;

<<:多行輸入;

set -C命令:禁止覆蓋重定向;

|:強(qiáng)制覆蓋重定向(與set -C相反);

set +C命令:解除禁止覆蓋重定向的設(shè)置;

? 管道符(|)作用是把前一個(gè)命令的執(zhí)行結(jié)果當(dāng)做后一個(gè)命令的輸入。

使用方式

1、重定向

【例1】把/etc/fstab文件內(nèi)容重定向到/tmp目錄下文件名為fstab.out

[root@Magedu ~]# cat /etc/fstab > /tmp/fstab.out

【例2】把hello world追加到/tmp/fstab.out文件尾部

[root@Magedu ~]# echo "hello world" >>/tmp/fstab.out 

【例3】禁止覆蓋重定向和強(qiáng)制重定向

[root@Magedu ~]# set -C

[root@Magedu ~]# echo "hello magedu" >/tmp/fstab.out

-bash: /tmp/fstab.out: cannot overwrite existing file

設(shè)置禁止覆蓋重定向后,可強(qiáng)制覆蓋重定向

[root@Magedu ~]# echo "hello magedu" >| /tmp/fstab.out

[root@Magedu ~]# cat /tmp/fstab.out

hello magedu

【例4】解除禁止覆蓋重定向設(shè)置

[root@Magedu ~]# set +C

[root@Magedu ~]# echo "www.vfuj.cn" > /tmp/fstab.out

【例5】把標(biāo)準(zhǔn)錯(cuò)誤覆蓋重定向到which.out文件

先看下只重定向標(biāo)準(zhǔn)輸出的情況:

[root@Magedu ~]# whch cat  > /tmp/which.out

bash: whch: command not found...

[root@Magedu ~]# cat /tmp/which.out

再看把標(biāo)準(zhǔn)錯(cuò)誤重定向的情況:

[root@Magedu ~]# whch cat  2> /tmp/which.out

[root@Magedu ~]# cat /tmp/which.out

bash: whch: command not found...

【例6】把標(biāo)準(zhǔn)錯(cuò)誤和標(biāo)準(zhǔn)輸出分別重覆蓋定向到不同的文件里,即標(biāo)準(zhǔn)錯(cuò)誤重定向到falt.txt文件,標(biāo)準(zhǔn)輸出重定向到correct.txt

[root@Magedu ~]# which cat 2> falt.txt > correct.txt

[root@Magedu ~]# cat correct.txt 

/usr/bin/cat

[root@Magedu ~]# cat falt.txt 



[root@Magedu ~]# wih cat 2> falt.txt > correct.txt

[root@Magedu ~]# cat falt.txt 

bash: wih: command not found...

[root@Magedu ~]# cat correct.txt 

【例7】合并標(biāo)準(zhǔn)輸出和標(biāo)準(zhǔn)錯(cuò)誤覆蓋重定向到out.txt文件里

[root@Magedu ~]# which cat &> out.txt

[root@Magedu ~]# cat out.txt

/usr/bin/cat

[root@Magedu ~]# hich cat &>> out.txt

[root@Magedu ~]# cat out.txt

/usr/bin/cat

bash: hich: command not found...

【例8】&>等價(jià)于2&>1,功能同上例

[root@Magedu ~]# which cat > out.txt 2>&1 

[root@Magedu ~]# cat out.txt 

/usr/bin/cat

[root@Magedu ~]# wich cat >> out.txt 2>&1 

[root@Magedu ~]# cat out.txt 

/usr/bin/cat

bash: wich: command not found...

2、輸入重定向和tr命令

tr命令是把字符集1轉(zhuǎn)換為字符集2

【例9】用輸入重定向的方式,把所有小寫字母轉(zhuǎn)換為大寫

[root@Magedu ~]# cat /etc/issue

\S

Kernel \r on an \m



[root@Magedu ~]# tr a-z A-Z </etc/issue

\S

KERNEL \R ON AN \M

從文件導(dǎo)入標(biāo)準(zhǔn)輸入

【例10】把out.txt文件里的內(nèi)容,寫到file.txt文件里

[root@Magedu ~]# cat >file.txt <out.txt

[root@Magedu ~]# cat file.txt 

/usr/bin/cat

bash: wich: command not found...

多行輸入:<<終止詞

【例11】屏幕隨便輸入幾行內(nèi)容,遇到END字樣結(jié)尾后,屏幕內(nèi)容自動(dòng)保存在f1.txt里

[root@Magedu ~]# cat > f1.txt <<END

> first

> scond

> third

> END

[root@Magedu ~]# cat f1.txt 

first

scond

third

【例12】使用mail命令root給lsj普通用戶發(fā)郵件,要求郵件標(biāo)題為”help”,郵件正文如下:

Hello, I am 用戶名,The system version is here,please help me to check it thanks!

操作系統(tǒng)版本信息

[root@Magedu ~]# mail -s "help" lsj <<EOF

> Hello, I am `who`,The system version is here,please help me to check it thanks!

> `cat /etc/redhat-release`

> EOF

[root@Magedu ~]# su - lsj

Last login: Thu May 31 00:29:49 EDT 2018 on pts/0

[lsj@Magedu ~]$ mail

Heirloom Mail version 12.5 7/5/10.  Type ? for help.

"/var/spool/mail/lsj": 7 messages 1 new 2 unread

    1 root                  Mon May 21 06:37  20/598   "ha"

    2 root                  Mon May 21 06:39  19/591   "ha"

    3 root                  Mon May 21 06:39  19/592   "ha"

    4 root                  Mon May 21 07:41  22/810   "he"

U  5 root                  Tue May 22 05:24  23/654   "he"

    6 root                  Tue May 22 05:25  22/648   "he"

>N  7 root                  Thu May 31 02:47  20/798   "he"

& 7

Message  7:

From root@Magedu.localdomain  Thu May 31 02:47:53 2018

Return-Path: <root@Magedu.localdomain>

X-Original-To: lsj

Delivered-To: lsj@Magedu.localdomain

Date: Thu, 31 May 2018 02:47:53 -0400

To: lsj@Magedu.localdomain

Subject: help

User-Agent: Heirloom mailx 12.5 7/5/10

Content-Type: text/plain; charset=us-ascii

From: root@Magedu.localdomain (root)

Status: R



Hello, I am root     pts/0        2018-05-30 21:20 (172.18.116.232)

root     pts/1        2018-05-31 00:25 (172.18.116.232),The system version is here,please help me to check it thanks!

CentOS Linux release 7.5.1804 (Core) 



& q

Held 7 messages in /var/spool/mail/lsj

[lsj@Magedu ~]$ 

3、管道符:|

【例13】把echo輸出的內(nèi)容,傳遞給tr命令,實(shí)現(xiàn)小寫字母轉(zhuǎn)換為大寫字母

[root@Magedu ~]# echo "this is test line" | tr a-z A-Z

THIS IS TEST LINE

【例14】一頁(yè)一頁(yè)的查看輸入

[root@Magedu ~]# ls -l /etc |less

total 1560

drwxr-xr-x.  3 root  root      101 May 14 11:29 abrt

-rw-r--r--.  1 root  root       16 May 14 11:44 adjtime

-rw-r--r--.  1 root  root     1518 Jun  7  2013 aliases

-rw-r--r--.  1 root  root    12288 May 14 11:46 aliases.db

...(省略)

-rw-r--r--.  1 root  root      524 Apr 12 15:23 auto.misc

:

文章來(lái)源于網(wǎng)絡(luò),侵刪!