• shell中的exec


    shell中exec命令

    1、find中的-exec参数

    在当前目录下(包含子目录),查找所有txt文件并找出含有字符串"bin"的行

    find ./ -name "*.txt" -exec grep "bin" {} \;

    在当前目录下(包含子目录),删除所有txt文件

    find ./ -name "*.txt" -exec rm {} \;

    Execute  command;  true  if 0 status is returned.  All following arguments to find are taken to be arguments to the command until  an  argument  consisting of `;' is encountered.  The string `{}' is replaced by the current file name being processed  everywhere  it occurs in the arguments to the command, not just in arguments where it is alone.

    2、shell的内置命令命令exec

    #!/bin/ksh

    export LOG=/tmp/test.log

    exec >> $LOG 2>&1

    ls -l kevin.txt

    exit 0

    exec [arg]

    If arg is present, executes arg in place of this shell.

    (arg will replace this shell).

    shell的内建命令exec将并不启动新的shell,而是用要被执行命令替换当前的shell进程,并且将老进程的环境清理掉,而且exec命令后的其它命令将不再执行。

    因此,如果你在一个shell里面,执行exec ls那么,当列出了当前目录后,这个shell就自己退出了,因为这个shell进程已被替换为仅仅执行ls命令的一个进程,执行结束自然也就退出了。为了避免这个影响我们的使用,一般将exec命令放到一个shell脚本里面,用主脚本调用这个脚本,调用点处可以用bash a.sh,(a.sh就是存放该命令的脚本),这样会为a.sh建立一个sub shell去执行,当执行到exec后,该子脚本进程就被替换成了相应的exec的命令。

    source命令或者”.”,不会为脚本新建shell,而只是将脚本包含的命令在当前shell执行。

    不过,要注意一个例外,当exec命令来对文件描述符操作的时候,就不会替换shell,而且操作完成后,还会继续执行接下来的命令。

    exec 3<&0:这个命令就是将操作符3也指向标准输入。

    原文

    [1]http://blog.chinaunix.net/uid-20652643-id-1906436.html

    [2]http://zhgw01.blog.163.com/blog/static/1041481220098944425489/

    [3]http://blog.csdn.net/clozxy/article/details/5818465

    [4]http://www.cnblogs.com/zhaoyl/archive/2012/07/07/2580749.html

  • 相关阅读:
    luogu4182 [USACO18JAN] Lifeguards P (单调队列优化dp)
    bzoj3277 串 (后缀数组+二分答案+ST表)
    [模板]后缀数组
    bzoj4361 isn (dp+树状数组+容斥)
    luogu4187 [USACO18JAN]Stamp Painting (dp)
    [USACO15DEC]高低卡(白金)High Card Low Card (Platinum)
    USACO环绕岛屿Surround the Islands 并查集 枚举暴力
    5.7 ~ 5.12 刷题列表
    5.4 ~ 5.6 刷题记录
    HNOI2012 永无乡 无旋Treap
  • 原文地址:https://www.cnblogs.com/mydomain/p/2924552.html
Copyright © 2020-2023  润新知