简体   繁体   中英

bash how to kill parent process, or exit from parent process from a function in bash module script

I have a few scripts:

functions.sh - has many functions defined including work() and abort() it looks like this:

#!/bin/bash

abort()
{
message=$1
echo  Error: $message ..Aborting >log
exit 7
}

work()
{
cp ./testfile ./test1           #./testfile doesnt exist so non zero status here

if [ $? -eq 0 ];then
        echo "variable_value"
else
        abort "Can not copy"
fi
}

parent.sh - parent script is the main script, it looks like this:

#!/bin/sh

. ./functions.sh

value=$(work)
echo "why is this still getting printed"

Basically i have many functions in the functions.sh file, and i am sourcing that file in parent.sh to make available all functions. Parent can call any function and any function in functions.sh can call abort at which point execution of parent should stop, but its not happening, parent.sh runs to the next step. Is there a way to get around this problem?

I realise that its happening due to assignment step value=$(work). But is there a way to abort execution just at abort function call in this case?

I'm not convinced that this behavior should be described as a "problem". As you noted, when you invoke work in a subshell, aborting from work just aborts the subshell. This is correct, expected behavior. Fortunately, the value returned by work in the subshell is available to the parent, so you can simply respond to it and write:

value=$(work) || exit

If work returns a non-zero value, then the script will exit with that same non-zero value.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM