Home > database >  "set -m" command in shell script
"set -m" command in shell script

Time:06-15

I was going through a shell script where set -m was used. My understanding is that set is used to get positional args.

For ex: set "SO community is very helpful". Now, if I do echo $1, I should get SO and so on for $2, $3...

After checking the command with help flag, I got "-m Job control is enabled."

My question is, what is the purpose of set -m in the following code?

set -m 
(
    current_hash="some_ha54_one"
    new_hash=$(cat file.txt)

    if [ $current_hash -ne new_hash ]; then
        pip install -r requirement.txt
    fi

    tmp="temp variable"
    export tmp

    bash some_bash_file.sh &
    
    wait
    
    bash some_other_bash_file.sh &
)

I understand (to the best of my knowledge) what I going on inside () but what is the use of set -m ?

CodePudding user response:

"Job control" enables features like bg and fg; signal-handling and file-descriptor routing changes intended for human operators who might use them to bring background tasks into the foreground to provide them with input; and the ability to refer to background tasks by job number instead of PID. The script segment you showed doesn't use these features, so the set -m call is presumably pointless.

These features are meant for human users, not scripts; and so in scripts they're off by default. In general, code that attempts to use them in scripts is buggy, and should be replaced with code that operates by PID. As an example, code that runs two scripts in parallel with each other, and then collects the exit status of each when they're finished without needing job control follows:

bash some_bash_file & some_pid=$!
bash some_other_file & some_other_pid=$!
wait "$some_pid"; some_rc=$?
wait "$some_other_pid"; some_other_rc=$?
  • Related