Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/api/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux 我的shell脚本在exec之后停止_Linux_Shell - Fatal编程技术网

Linux 我的shell脚本在exec之后停止

Linux 我的shell脚本在exec之后停止,linux,shell,Linux,Shell,我正在编写一个shell脚本,如下所示: for i in $ACTIONS_DIR/* do if [ -x $i ]; then exec $i nap fi done 现在,我试图实现的是列出$ACTIONS\u DIR中的每个文件,以便能够执行它。$ACTIONS\u DIR下的每个文件都是另一个shell脚本 现在,这里的问题是,在使用exec之后,脚本停止,不会转到行中的下一个

我正在编写一个shell脚本,如下所示:

 for i in $ACTIONS_DIR/*
    do
            if [ -x $i ]; then
                    exec $i nap
            fi
    done
现在,我试图实现的是列出$ACTIONS\u DIR中的每个文件,以便能够执行它。$ACTIONS\u DIR下的每个文件都是另一个shell脚本


现在,这里的问题是,在使用exec之后,脚本停止,不会转到行中的下一个文件。知道为什么会这样吗?

exec
将PID控制权转移到正在执行的程序上。这主要用于脚本,其唯一目的是为该程序设置选项。一旦点击
exec
,脚本中低于它的任何内容都不会执行

此外,您应该尝试一些引用技巧:

for i in $ACTIONS_DIR/*
    do
        if [ -x "$i" ]; then
                "./$i" nap
        fi
done
您还可以研究使用
find(1)
进行此操作:

find $ACTIONS_DIR \
    -maxdepth 1 \
    -type f \
    -perm +0111 \
    -exec {} nap \;

exec
替换shell进程。如果您只想将命令作为子进程调用,请将其删除。

exec永远不会返回调用方。试试看

        if [ -x $i ]; then
                ./$i nap
        fi
“$i”
,否则如果程序名包含空格,您将有错误行为。“$ACTIONS\u DIR”/*中的i的
如果我们不知道目录名不包含空格(或者如果我们不知道/控制
IFS
的值),则会更安全一些。另请参阅: