程序:指令 +數據linux
讀取數據:Inputnginx
輸出數據:Outputgit
打開的文件都有一個fd:file description(文件描述符)shell
Linux給程序提供三種I/O設備centos
標準輸入(STDIN)-0 默認接受來自鍵盤的輸入bash
標準輸出(StDOUT) -1 默認給輸出到終端窗口less
標準錯誤)(STDRR)-2 默認輸出到終端窗口dom
I/O重定向:改變默認位置ide
STDOUT和STDERR能夠被重定向到文件工具
命令 操做符號 文件名
支持的操做符號包括
> 把STDOUT重定向到文件
2> 把STDERR重定向到文件
&> 把全部輸出重定向到文件
>文件內容會被覆蓋
set -C禁止將內容覆蓋已有文件,但可追加
> | file 強制覆蓋
set +C容許覆蓋
>>原有內容基礎上,追加內容
2>覆蓋重定向錯誤輸出數據流
2>>追加劇定向錯誤輸出數據流
標準輸出和錯誤輸出各自定向至不一樣位置
COMMAND > /path/to/file.out 2> /path/to/error.out
合併標準輸出和錯誤輸出爲同一個數據流進行重定向
&>覆蓋重定向
&>>追加劇定向
COMMAND > /path/to/file.out 2>&1(順序很重要)
COMMAND >>/path/to/file.out 2>&1
():合併多個程序的STDOUT
(cal 2007;cal 2008)> all.txt
1 [root@centos6 ~]#ls /error /data > log1 2>&1 2 [root@centos6 ~]#ls /error /data 2>&1 > log2 3 ls: cannot access /error: No such file or directory 4 [root@centos6 ~]#ls /error /data &> log3 5 [root@centos6 ~]#ls /error /data 2>log4 >&2
[root@centos6 ~]#cat f1 #bin/bash #i want to print something echo "hello word" [root@centos6 ~]#cat <f1 >f1 [root@centos6 ~]#cat f1
tr命令
tr轉換和刪除字符
tr[OPTION]...SET1 [SET2]
選項
-c -C——complement: 去字符集的補集
-d——delete: 刪除全部屬於第一字符集的字符
-s——squeezu - repeats: 把連續重複的字符以單獨一個字符表示
-t——truncate - set1: 將第一個字符集對應字符轉化爲第二字符集對應的字符
[:alnum:]:字母和數字 [:alpha:]:字母 [:cntrl:]:控制(非打印)字符 [:digit:]:數字
[:graph:]:圖形字符 [:lower:]:小寫字母 [:print:]:可打印字符 [:punct:]:標點符號
[:space:]:空白字符 [:upper:]:大寫字母 [:xdigit:]:十六進制字符
2、把I/O重定向至文件
使用<來重定向標準輸入
tr 'a-z' 'A-Z' < /etc/issue
該命令會把/etc/issue中的小寫字符都轉換成大寫字符
tr -d abc < /etc/fstab 刪除fstab文件中全部abc中任意字符
cat > file
mage
wangxiaochun
按Ctrl + d 離開,能夠使用文件來代替鍵盤輸入
Cat > filea <fileb
使用「<<終止詞」命令從鍵盤把多行重導向給STDIN
直到 終止詞 位置的全部文本都發送給STDIN
有事被稱爲就地文本(heretext)
COMMAND1 | COMMAND2 | COMMAND3
將命令1的STDOUT發送給命令2的STDIN,命令2的STDIN發送到命令3的STDIN
STDERR默認不能經過管道轉發,可利用2>&1或 |&實現
最後一個命令會在當前shell進程的字shell進程中執行用來組合多種工具的功能
ls | tr 'a-z' 'A-Z'
less:一頁一頁地查看輸入
ls -l /etc | less
mail:經過電子郵件發送輸入
echo "test email" | mail -s "test' user@example.com
ipr:把輸入發送給打印機
echo "test print" | lpr -P printer_name
示例:
將、home裏的文件打包,但打包帶數據不是記錄到文件,而是傳送到stdout,通過管道後,將tar -cvf - /home 傳送給後面的 tar -xvf -,後面這個-則是取前一個命令的stdout,所以,就不須要使用臨時的file了
[root@centos6 ~]#tar -cvf - /home | tar -xvf - tar: Removing leading `/' from member names /home/ /home/gentoo/ /home/gentoo/.gnome2/ /home/gentoo/.bashrc /home/gentoo/.mozilla/ /home/gentoo/.mozilla/plugins/ /home/gentoo/.mozilla/extensions/ /home/gentoo/.bash_logout /home/gentoo/.bash_profile /home/varnish/ /home/varnish/.gnome2/ /home/varnish/.bashrc /home/varnish/.mozilla/ /home/varnish/.mozilla/plugins/ /home/varnish/.mozilla/extensions/ /home/varnish/.bash_logout home/ /home/varnish/.bash_profile /home/nginx/ /home/nginx/.gnome2/ /home/nginx/.bashrc /home/nginx/.mozilla/ /home/nginx/.mozilla/plugins/ /home/nginx/.mozilla/extensions/ /home/nginx/.bash_logout /home/nginx/.bash_profile home/gentoo/ home/gentoo/.gnome2/ home/gentoo/.bashrc home/gentoo/.mozilla/ home/gentoo/.mozilla/plugins/ home/gentoo/.mozilla/extensions/ home/gentoo/.bash_logout home/gentoo/.bash_profile home/varnish/ home/varnish/.gnome2/ home/varnish/.bashrc home/varnish/.mozilla/ home/varnish/.mozilla/plugins/ home/varnish/.mozilla/extensions/ home/varnish/.bash_logout home/varnish/.bash_profile home/nginx/ home/nginx/.gnome2/ home/nginx/.bashrc home/nginx/.mozilla/ home/nginx/.mozilla/plugins/ home/nginx/.mozilla/extensions/ home/nginx/.bash_logout home/nginx/.bash_profile
命令1 | tee [-a] 文件名 | 命令2
把命令1的STDOUT保存在文件中,做爲命令2的輸入
-a 追加
使用
保存不一樣階段的輸出
複雜管道的故障排除
同時查看和記錄輸出
一、將/etc/issue文件中的內容轉換爲大寫文件後保存至/tem/issue.out文件中
[root@centos6 /]#mkdir tem [root@centos6 /]#tr 'a-z' 'A-Z' < /etc/issue >/tem/issue.out [root@centos6 /tem]#cat issue.out CENTOS RELEASE 6.9 (FINAL) KERNEL \R ON AN \M
二、將當前系統登陸用戶的信息轉換爲大寫後保存至/tmp/who.out文件中
[root@centos6 ~]#who | tr 'a-z' 'A-Z' >/tmp/who.out [root@centos6 ~]#cat /tmp/who.out ROOT TTY1 2018-03-30 08:01 (:0) ROOT PTS/0 2018-03-30 08:01 (192.168.190.1)
三、一個linux用戶給root發郵件,要求郵件標題爲」help」,郵件正文以下:
Hello, I am 用戶名,The system version is here,please help me to check it ,thanks!
操做系統版本信息
[root@centos6 ~]#mail -s "help" root <<end > hello,I am $USER, > The system version is here,please hellp me to check it,thanks > OS version:`lsb_release -a` > end [root@centos6 ~]#mail Heirloom Mail version 12.4 7/29/08. Type ? for help. "/var/spool/mail/root": 5 messages 1 new 3 unread U 1 Anacron Sat Mar 24 09:13 26/1249 root Thu Mar 29 09:57 20/695 root Fri Mar 30 08:11 20/696 U 4 Mail Delivery System Fri Mar 30 08:11 74/2466 >N 5 root Fri Mar 30 08:36 24/919 & 5 Message 5: From root@centos6.localdomain Fri Mar 30 08:36:25 201 Return-Path: <root@centos6.localdomain> X-Original-To: root Delivered-To: root@centos6.localdomain Date: Fri, 30 Mar 2018 08:36:25 +0800 To: root@centos6.localdomain Subject: help User-Agent: Heirloom mailx 12.4 7/29/08 Content-Type: text/plain; charset=us-ascii From: root@centos6.localdomain (root) Status: R hello,I am root, The system version is here,please hellp me to check it ,thanks OS version:LSB Version: :base-4.0-amd64:base-4.0-noarc h:core-4.0-amd64:core-4.0-noarch:graphics-4.0-amd64:gr aphics-4.0-noarch:printing-4.0-amd64:printing-4.0-noar ch Distributor ID: CentOS Description: CentOS release 6.9 (Final) Release: 6.9 Codename: Final
四、將/root/下文件列表,顯示成一行,並文件名之間用空格隔開
[root@centos6 ~]#ls | tr '\n' ' ' anaconda-ks.cfg Desktop Documents Downloads f1 home install.log install.log.syslog log1 log2 log3 log4 motd Music Pictures Public rm Templates Videos
五、計算1+2+3+..+99+100的總和
[root@centos6 ~]#echo {1..100} | tr ' ' '+' |bc 5050 [root@centos6 ~]#seq -s + 1 100 | bc 5050
六、刪除Windows文本文件中的‘^M’字符
[root@centos6 ~]#tr -d '\r' < win.txt a b c
七、處理字符串「xt.,l 1 jr#!$mn 2 c*/fe 3 uz 4」,只保留其中的數字和空格
[root@centos6 ~]#echo "xt.,l 1 jr#win.txtmn 2 c*/fe 3 uz 4" | tr -d '[:alpha:]''[:punct:]' 1 2 3 4
八、將PATH變量每一個目錄顯示在獨立的一行
[root@centos6 ~]#echo $PATH | tr ':' '\n' /usr/lib64/qt-3.3/bin /usr/local/sbin /usr/local/bin /sbin /bin /usr/sbin /usr/bin /root/bin [root@centos6 ~]#
九、將指定文件中0-9分別替代成a-j
[root@centos6 ~]#echo {0..20} | tr '0-9' 'a-j' a b c d e f g h i j ba bb bc bd be bf bg bh bi bj ca
十、將文件/etc/centos-release中每一個單詞(由字母組成)顯示在獨立的一行,並沒有空行
[root@centos6 ~]#cat /etc/centos-release | tr ' ' '\n'CentOS release 6.9 (Final)