My shell script stops after exec

后端 未结 3 1826
栀梦
栀梦 2020-11-30 12:54

I\'m writing a shell script that looks like this:

 for i in $ACTIONS_DIR/*
    do
            if [ -x $i ]; then
                    exec $i nap
                     


        
3条回答
  •  庸人自扰
    2020-11-30 13:31

    exec transfers control of the PID over to the program you're exec'ing. This is mainly used in scripts whose sole purpose is to set up options to that program. Once the exec is hit, nothing below it in the script is executed.

    Also, you should try some quoting techniques:

    for i in $ACTIONS_DIR/*
        do
            if [ -x "$i" ]; then
                    "./$i" nap
            fi
    done
    

    You might also look into using find(1) for this operation:

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

提交回复
热议问题