当前位置: 移动技术网 > 科技>操作系统>Linux > linux系统管理-输入输出

linux系统管理-输入输出

2019年07月04日  | 移动技术网科技  | 我要评论

目录

linux系统管理-输入输出

重定向

将原本要输出到屏幕上的数据信息,重新定向到指定的文件中

运行程序,或者输入一个命令:默认打开4个文件描述符

标准输入与输出

名称 文件描述符 作用
标准输入(stdin) 0 通常键盘,也可以是其他文件或者命令的输出的内容可以作为标准输入
标准输出(stdout) 1 默认输出到屏幕
错误输出(stderr) 2 默认输出到屏幕
文件名称(filename) 3+
/dev/stderr(错误输出) -> /proc/self/fd/2            2 -> /dev/pts/0
/dev/stdin(标准输入) -> /proc/self/fd/0             0 -> /dev/pts/0
/dev/stdout(标准输出) -> /proc/self/fd/1            1 -> /dev/pts/0
输出重定向分类
  • 重定向的特性:覆盖文件

1>:标准输出重定向,将命令执行的正确结果输出到指定的文件或者设备中

2>:错误输出重定向

  • 追加重定向:不会覆盖文件

1>>:标准输出追加重定向,将命令执行的正确结果追加输出到文件末尾

2>>:错输出追加重定向,将命令执行的错误结果追加输出到文件末尾

<< :标准输入重定向,将键盘敲的内容,输入到命令或者文件中

输出重定向的使用
#将正确的内容追加到文件中,错误会输出到屏幕(不会覆盖源文件)
echo "this is network conf" >> abc

#将错误的内容输出到文件中,正确的会输出到屏幕(会覆盖源文件)
find /etc -name "*.conf" 2>b
find /etc -name "*.conf" 1>a 2>b

#合并输出,错误的正确的内容都会输出到一个文件(会覆盖源文件)
find /etc -name "*.conf" >c 2>&1
find /etc -name "*.conf" >c 2>c
find /etc -name "*.conf" &>c

#将错误输出重定向到 ‘黑洞’,正确内容输出到屏幕/dev/pts/x
ls /root/ /err 2>/dev/null

#将错误输出重定向到 ‘黑洞’,正确内容输出到1.txt文件中
ls /root/ /err >1.txt 2>/dev/null

输入的重定向使用

< :0<

<<:0<<

案例1:
    cat >> zls.txt <<eof
    zls
    qiudao
    eof
    
    案例2:
    [root@zls ~]# mail zls < /etc/passwd
    
    案例3:
    [root@oldboyedu ~]# grep 'root'
    rppr
    qwe
    rootasdasdadzxczxc
    rootasdasdadzxczxc
    ^c
    
    案例4:
    [root@oldboyedu ~]# dd if=/dev/zero of=/file1.txt bs=1m count=20
    20+0 records in
    20+0 records out
    20971520 bytes (21 mb) copied, 0.0260574 s, 805 mb/s

    [root@oldboyedu ~]# dd </dev/zero >/file2.txt bs=1m count=20
    20+0 records in
    20+0 records out
    20971520 bytes (21 mb) copied, 0.011896 s, 1.8 gb/s


    案例5:
    恢复mysql数据
    [root@zls ~]# mysql -uroot -p123 < bbs.sql
    
    
    案例6:利用重定向建立多行文件
    [root@oldboyedu ~]# cat >file1

    案例7:
    #!/bin/sh
    menu(){
        cat <<eof
        +------------+
        | 1 | apple  |
        +---+--------+
        | 2 | pear   |
        +---+--------+
        | 3 | banana |
        +---+--------+
        | 4 | cherry |
        +---+--------+
    eof
    read -p "please input a num: " fruit
    }
    
    usage(){
        echo "usage:请输入水果编号"
        exit 1
    }
    
    color(){
    case "$fruit" in
    1)
        echo -e "\e[1;31mapple \e[0m"
        ;;
    2)
        echo -e "\e[1;20mpear \e[0m"
        ;;
    3)
        echo -e "\e[1;33mbanana \e[0m"
        ;;
    4)
        echo -e "\e[1;35mcherry \e[0m"
        ;;
    *)
        usage
    esac
    }
    menu
    color
    
    案例8:多条命令重定向
    [root@oldboyedu ~]# (ls;date) > a.txt
    
    
    案例9:后台进程重定向
    (while :; do date; sleep 2; done) &>/dev/null &

管道技术 |

连接多个命令,将管道符左侧的标准输出,交给管道符右侧的命令标准输入

案例1:
    将/etc/passwd 中的uid取出并按大小排序
    [root@oldboyedu ~]# awk -f : '{print $3}' /etc/passwd|sort -n

案例2:
    统计当前/etc/passwd 中用户使用的 shell 类型
    [root@oldboyedu ~]# awk -f : '{print $nf}' /etc/passwd|sort |uniq|wc -l
    5
案例4:统计网站访问量top20
    [root@driver-zeng nginx]# awk '{print $1}' driverzeng.com_access.log|sort       |uniq -c|sort -nr|head -20
    
案例5:取出cpu已使用的百分比,只显示数字
    [root@oldboyedu ~]# df -h |awk -f '[ %]+' 'nr==2 {print $5}'
    4

tee:相当于管道符

[root@zls ~]# date > date.txt 
[root@zls ~]# date |tee date.txt

参数传递xargs

将参数列表转换成小块分段传递给其他命令
读入stdin的数据转换为参数添加至命令后面
让一些不支持管道的命令可以使用管道。

注意:

1.在管道后面的命令,都不应该在写文件名
2.在管道中只有标准输出才可以传递下一个命令, 标准错误输出会直接输出终端显示, 建议在使用管道前将标准错误输出重定向。
例如: find /etc -name "*.conf" 2>/dev/null | grep rc
3.有些命令不支持管道技术, 但是可以通过xargs来实现管道传递。
例如: which cat|xargs ls-l
例如: ls |xargs rm -rvf
例如: ls |xargs cp -rvft /tmp/ -> ls | xargs -i {} cp -rvf {} /tmp
例如: ls |xargs mv -t /tmp/ -> ls | xargs -i {} mv {} /tmp

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网