• bash手册 之重定向原理与实现


    http://www.gnu.org/software/bash/manual/bashref.html#Redirections

    http://www.cnblogs.com/weidagang2046/p/io-redirection.html 原理与实现

    http://blog.csdn.net/taiyang1987912/article/details/39401265

    [root@server1 tmp]# cd /dev/fd
    [root@server1 fd]# ll
    总用量 0
    lrwx------ 1 root root 64 6月 4 12:17 0 -> /dev/pts/0
    lrwx------ 1 root root 64 6月 4 12:17 1 -> /dev/pts/0
    lrwx------ 1 root root 64 6月 4 12:17 2 -> /dev/pts/0
    lrwx------ 1 root root 64 6月 4 12:17 255 -> /dev/pts/0

    #使用exec将stdin重定向到文件
    #!/bin/bash
    
    exec 8<&0           #FD 8是FD 0的副本,用于恢复标准输入
    exec < file         #将标准输入重定向到file
    read a              #读取file的第一行
    read b              #读取file的第二行
    echo "read has read $$"
    echo "read has read $PPID"
    sleep 100
    echo "----------------"
    echo $a             #标准输出
    echo $b             #标准输出

    该进程的文件描述符FD 变为如下

    [root@server1 fd]# ll
    总用量 0
    lr-x------ 1 root root 64 6月 4 13:03 0 -> /root/file
    lrwx------ 1 root root 64 6月 4 13:03 1 -> /dev/pts/0
    lrwx------ 1 root root 64 6月 4 13:03 2 -> /dev/pts/0
    lr-x------ 1 root root 64 6月 4 13:03 254 -> /root/a.sh
    lrwx------ 1 root root 64 6月 4 13:03 255 -> /dev/pts/0
    lrwx------ 1 root root 64 6月 4 13:03 8 -> /dev/pts/0

    echo "close FD 8:"
    exec 0<&8 8<&- #将FD 8复制到FD 0,恢复FD 0,并关闭FD 8,其他进程可以重复使用FD 8
    echo -n "Enter Data:"
    read c #read从标准输入读取数据
    echo $c
    echo $$
    echo $PPID
    sleep 100

    [root@server1 4598]# ll fd
    总用量 0
    lrwx------ 1 root root 64 6月 4 13:11 0 -> /dev/pts/0
    lrwx------ 1 root root 64 6月 4 13:11 1 -> /dev/pts/0
    lrwx------ 1 root root 64 6月 4 13:11 2 -> /dev/pts/0
    lr-x------ 1 root root 64 6月 4 13:11 254 -> /root/a.sh
    lrwx------ 1 root root 64 6月 4 13:11 255 -> /dev/pts/0

     
    #eval重新提交shell
    #!/bin/bash
    
    while read NAME VALUE     #第一列作为变量名,第二列作为变量值        //对应文件中的两列
    do
    eval "${NAME}=${VALUE}"   #第1轮变量替换,eval重新提交shell完成赋值操作     
    done < evalsource         #输入重定向
    echo "var1=$var1"         #变量赋值
    echo "var2=$var2"

    eval "${NAME}=${VALUE}" 为一段可执行的命令,而不是字符串

    [root@server1 ~]# eval "cc=100"
    [root@server1 ~]# echo $cc
    100


    [root@server1 ~]# echo "dd=100"
    dd=100



    
    [root@server1 ~]# x=100
    [root@server1 ~]# ptrx=x
    [root@server1 ~]# eval echo $$ptrx
    100
    [root@server1 ~]# eval $ptrx=50
    [root@server1 ~]# echo $x
    50
    [root@server1 ~]# echo $ptrx
    x
  • 相关阅读:
    [GUIDE] How to Setup Ubuntu 16.04 LTS Xenial Xerus for Compiling Android ROMs
    设置Ubuntu 16.04 LTS的Unity启动器的位置命令
    sed系列:行或者模式匹配删除特定行
    HDOJ 4923 Room and Moor
    Office365client通过本地方式批量部署(即点即用部署)
    hdu 1867 A + B for you again
    Photoshop经常使用快捷键(2)
    SQL_为表和列加凝视
    从头认识java-17.5 堵塞队列(以生产者消费者模式为例)
    Unity5 怎样做资源管理和增量更新
  • 原文地址:https://www.cnblogs.com/zengkefu/p/5558539.html
Copyright © 2020-2023  润新知