109.linux标椎输入和标准输出

1.linux标椎输入和标准输出

1.简介
    标准输入0    从键盘获得输入 /proc/self/fd/0 
    标准输出1    输出到屏幕(即控制台) /proc/self/fd/1 
    错误输出2    输出到屏幕(即控制台) /proc/self/fd/2 
    [root@localhost ~]# ll /proc/self/fd/
    lrwx------ 1 root root 64 Nov  6 09:09 0 -> /dev/pts/2
    lrwx------ 1 root root 64 Nov  6 09:09 1 -> /dev/pts/2
    lrwx------ 1 root root 64 Nov  6 09:09 2 -> /dev/pts/2
    lr-x------ 1 root root 64 Nov  6 09:09 3 -> /proc/27136/fd

2.linux中的空文件黑洞
    /dev/null表示黑洞,所有往这个文件里面写入的内容都会丢失,俗称“黑洞” 
    [root@localhost test]# ll /dev/null
    crw-rw-rw- 1 root root 1, 3 Nov  2 15:44 /dev/null

3.案例
    1)[root@localhost test]# echo "i am a boy" > /dev/null
    # 将输出输入到/dev/null中发现没了

    2)2>/dev/null
    # 意思就是把错误输出到"黑洞"

    3)>/dev/null 2>&1
    # 默认情况是1,也就是等同于1>/dev/null 2>&1。意思就是把标准输出重定向到“黑洞”,还把错误输出2重定向到标准输出1,也就是标准输出和错误输出都进了“黑洞” 

    4)解析:先将错误输出重定向到标椎输出,再将标椎输出写入a.txt(这个时候标椎输出已经经过重定向,内容给了错误输出,所以文件为空)
    [root@localhost test]# echo "i am a boy" >a.txt 2>&1
    [root@localhost test]# cat a.txt 
    i am a boy

    5)解析: 先将标椎输出重定向到错误输出,再将标椎输出写入a.txt(这个时候标椎输出已经经过重定向,内容给了错误输出,所以文件为空)
    [root@localhost test]# echo "i am a boy" >a.txt 1>&2
    i am a boy
    [root@localhost test]# cat a.txt 

    6)解析:先将标椎输出先写到a.txt文件中,再将标椎输出重定向到错误输出
    [root@localhost test]# echo "i am a boy" 1>&2  >a.txt
    [root@localhost test]# cat a.txt 
    i am a boy

    7)解析:先将标椎输出先写到a.txt文件中,再将错误输出重定向到标椎输出
    [root@localhost test]# echo "i am a boy" 2>&1  >a.txt
    [root@localhost test]# cat a.txt 
    i am a boy

4.升级版脚本测试
    [root@localhost test]# cat test.sh 
    cp -s xxx xxx   # 专门写的错误命令
    echo "i am a boy"

    1)解析:先将错误输出重定向到标椎输出,再将标椎输出写到a.txt文件中
    [root@localhost test]# sh ./test.sh >a.txt 2>&1
    [root@localhost test]# cat a.txt 
    cp: cannot stat ‘xxx’: No such file or directory
    i am a boy

    2)先将标椎输出重定向到错误输出,再将标椎输出先写到a.txt文件中(由于标椎输出已经重定向到错误输出,这个时候标椎输出没内容,文件a.txt为空)
    [root@localhost test]# sh ./test.sh >a.txt 1>&2
    cp: cannot stat ‘xxx’: No such file or directory
    i am a boy
    [root@localhost test]# cat a.txt

    3)解析:先将标椎输出先写到a.txt文件中,再将标椎输出重定向到错误输出
    [root@localhost test]# sh ./test.sh 1>&2  >a.txt
    cp: cannot stat ‘xxx’: No such file or directory
    [root@localhost test]# cat a.txt 
    i am a boy

    4)先将标椎输出先写到a.txt文件中,再将错误输出重定向到标椎输出
    [root@localhost test]# sh ./test.sh 2>&1  >a.txt
    cp: cannot stat ‘xxx’: No such file or directory
    [root@localhost test]# cat a.txt 
    i am a boy

    这个时候大家有没有发现一个规律,每次都是最后面的先执行,之后才是倒数第二个.

    5.接下来看看下面的和你预想的一样么?
    1)
    [root@localhost test]# sh ./test.sh >/dev/null 2>&1   # 输出全部进入/dev/null
    [root@localhost test]# sh ./test.sh >/dev/null 1>&2   # 输出全部进入打印到屏幕
    cp: cannot stat ‘xxx’: No such file or directory
    i am a boy

    2)
    [root@localhost test]# sh ./test.sh 2>&1 >/dev/null  # 标椎输入进入黑洞,错误输出打印
    cp: cannot stat ‘xxx’: No such file or directory
    [root@localhost test]# sh ./test.sh 1>&2 >/dev/null # 标椎输入进入黑洞,错误输出打印
    cp: cannot stat ‘xxx’: No such file or directory

6.试一试这个
    1)先写入标椎输出,在写出错误输出
    [root@localhost test]# sh ./test.sh 1>a.txt 2>a.txt 
    [root@localhost test]# cat a.txt 
    i am a boy
    stat ‘xxx’: No such file or directory

    2)先写入错误输出,在写出标椎输出(2>&1像不像将错误输出插入到了标椎输出前面)
    [root@localhost test]# sh ./test.sh >a.txt 2>&1
    [root@localhost test]# cat a.txt 
    cp: cannot stat ‘xxx’: No such file or directory
    i am a boy

    这个时候有意思的是不是来了,我们在打印输出的时候还可以调整先后顺序.
7.测试
    [root@localhost test]# cat test.sh   # 脚本内容如下
    mp xxx xxx
    echo "我是中国人"
    mp xxx xxx
    echo "i an a boy"
    mp xxx xxx
    mp xxx xxx

    1)我们真的可以调整错误输出和标椎输出的先后顺序
    [root@localhost test]# sh ./test.sh 1>a.txt 2>a.txt 
    [root@localhost test]# cat a.txt 
    我是中国人
    i an a boy
    and not found
    ./test.sh: line 3: mp: command not found
    ./test.sh: line 5: mp: command not found
    ./test.sh: line 6: mp: command not found

    2)2>&1这种方式不能调整错误输出和标椎输出的先后顺序
    [root@localhost test]# sh ./test.sh >a.txt 2>&1
    [root@localhost test]# cat a.txt 
    ./test.sh: line 1: mp: command not found
    我是中国人
    ./test.sh: line 3: mp: command not found
    i an a boy
    ./test.sh: line 5: mp: command not found
    ./test.sh: line 6: mp: command not found
posted @ 2020-11-06 10:07  楠海  阅读(82)  评论(0编辑  收藏  举报