Skip to main content
added 204 characters in body
Source Link
Alexander Mills
  • 10.9k
  • 27
  • 120
  • 214

this is the generic script you're looking for. The only downside is your commands are in quotes which means syntax highlighting via your IDE will not really work. Otherwise, I have tried a couple of the other answers and this is the best one. This answer incorporates the idea of using wait <pid> given by @Michael but goes a step further by using the trap command which seems to work best.

#!/usr/bin/env bash

set -m # allow for job control
EXIT_CODE=0;  # exit code of overall script

function foohandleJobs() {
     for job in `jobs -p`; do
         echo "CHLD"PID exit=> code${job}"
 is        CODE=0;
         wait ${job} || CODE=$?"
         if [[ $?"${CODE}" >!= 0"0" ]]; then
         echo "at"At least one test failed";failed with exit code => ${CODE}" ;
         EXIT_CODE=1;
         fi
     done
}

trap 'foo''handleJobs' CHLD
   # trap command is the key part
DIRN=$(dirname "$0");

commands=(
    "echo"{ 'x';echo 'a'; exit 1;"1; }"
    "echo"{ 'y';echo 'b'; exit 0;"0; }"
    "echo"{ 'z';echo 'c'; exit 2;"2; }"
)

clen=`expr "${#commands[@]}" - 1` # get length of commands - 1

for i in `seq 0 "$clen"`; do
    (echo "${commands[$i]}" | bash) &   # run the command via bash in subshell
    echo "$i ith command has been issued as a background job"
done

wait; # wait for all subshells to finish
wait;

echo "EXIT_CODE => $EXIT_CODE"
exit "$EXIT_CODE"
 
# end

thanks to @michael homer for getting me on the right track, but using trap command is the best approach AFAICT.

this is the generic script you're looking for. The only downside is your commands are in quotes which means syntax highlighting via your IDE will not really work.

#!/usr/bin/env bash

set -m # allow for job control
EXIT_CODE=0;  # exit code of overall script

function foo() {
   echo "CHLD exit code is $?"
   if [[ $? > 0 ]]; then
      echo "at least one test failed";
      EXIT_CODE=1;
   fi
}

trap 'foo' CHLD
 
DIRN=$(dirname "$0");

commands=(
    "echo 'x'; exit 1;"
    "echo 'y'; exit 0;"
    "echo 'z'; exit 2;"
)

clen=`expr "${#commands[@]}" - 1` # get length of commands - 1

for i in `seq 0 "$clen"`; do
    (echo "${commands[$i]}" | bash) &   # run the command via bash in subshell
    echo "$i ith command has been issued as a background job"
done

# wait for all to finish
wait;

echo "EXIT_CODE => $EXIT_CODE"
exit "$EXIT_CODE"
 
# end

thanks to @michael homer for getting me on the right track

this is the generic script you're looking for. The only downside is your commands are in quotes which means syntax highlighting via your IDE will not really work. Otherwise, I have tried a couple of the other answers and this is the best one. This answer incorporates the idea of using wait <pid> given by @Michael but goes a step further by using the trap command which seems to work best.

#!/usr/bin/env bash

set -m # allow for job control
EXIT_CODE=0;  # exit code of overall script

function handleJobs() {
     for job in `jobs -p`; do
         echo "PID => ${job}"
         CODE=0;
         wait ${job} || CODE=$?
         if [[ "${CODE}" != "0" ]]; then
         echo "At least one test failed with exit code => ${CODE}" ;
         EXIT_CODE=1;
         fi
     done
}

trap 'handleJobs' CHLD  # trap command is the key part
DIRN=$(dirname "$0");

commands=(
    "{ echo 'a'; exit 1; }"
    "{ echo 'b'; exit 0; }"
    "{ echo 'c'; exit 2; }"
)

clen=`expr "${#commands[@]}" - 1` # get length of commands - 1

for i in `seq 0 "$clen"`; do
    (echo "${commands[$i]}" | bash) &   # run the command via bash in subshell
    echo "$i ith command has been issued as a background job"
done

wait; # wait for all subshells to finish

echo "EXIT_CODE => $EXIT_CODE"
exit "$EXIT_CODE"
# end

thanks to @michael homer for getting me on the right track, but using trap command is the best approach AFAICT.

use trap to get exit code of sub shells
Source Link
Alexander Mills
  • 10.9k
  • 27
  • 120
  • 214

this is the generic script you're looking for. The only downside is your commands are in quotes which means syntax highlighting via your IDE will not really work.

    #!/usr/bin/env bash
    
    arr=()set -m # holds PIDs
 allow for job control
EXIT_CODE=0;  # exit commands=(
code of overall script

function foo() {
   "echoecho 'x';"CHLD exit 1;"
code is $?"
   if [[ $? "echo> 'y';0 exit]]; 2;"then
      echo "at "echoleast 'z';one exittest 0;"failed";
    )
    EXIT_CODE=1;
    commands_len="${#commands[@]}"  # get length of commandsfi
    clen=`expr ${commands_len} - 1`  # subtract one from length
    
  trap 'foo' CHLD
    for i in `seq 0 "$clen"`
   DIRN=$(dirname do"$0");
       # run the command via bash
commands=(
 in subshell and push"echo PID'x'; toexit array1;"
    "echo 'y'; exit (echo0;"
 "${commands[$i]}" | bash) &"echo arr+=($!)'z'; exit 2;"
    done)
    
   clen=`expr len="$"${#arr[@]#commands[@]}" - 1` # get length of arr
    
    
    EXIT_CODE=0;  # exit code ofcommands overall- script1
    
    for i in `seq 0 "$len"`
   "$clen"`; do
        pid="${arr[$i]}";
        wait(echo $"${pidcommands[$i]} ; CODE="$?"
  | bash) &   # ifrun [[the ${CODE}command >via 0bash ]];in thensubshell
           echo "at least one test failed";
 "$i ith command has been issued as a background EXIT_CODE=1;job"
     done

# wait for all to fifinish
    donewait;
    
    echo "EXIT_CODE => $EXIT_CODE"
    exit "$EXIT_CODE"

    # end

thanks to @michael homer for getting me on the right track

this is the generic script you're looking for. The only downside is your commands are in quotes which means syntax highlighting via your IDE will not really work.

    #!/usr/bin/env bash
    
    arr=()  # holds PIDs
    
    commands=(
        "echo 'x'; exit 1;"
        "echo 'y'; exit 2;"
        "echo 'z'; exit 0;"
    )
    
    commands_len="${#commands[@]}"  # get length of commands
    clen=`expr ${commands_len} - 1`  # subtract one from length
    
    
    for i in `seq 0 "$clen"`
    do
       # run the command via bash in subshell and push PID to array
       (echo "${commands[$i]}" | bash) & arr+=($!)  
    done
    
    len="${#arr[@]}"  # get length of arr
    
    
    EXIT_CODE=0;  # exit code of overall script
    
    for i in `seq 0 "$len"`
    do
        pid="${arr[$i]}";
        wait ${pid} ; CODE="$?"
        if [[ ${CODE} > 0 ]]; then
           echo "at least one test failed";
          EXIT_CODE=1;
          fi
    done
    
    echo "EXIT_CODE => $EXIT_CODE"
    exit "$EXIT_CODE"

    # end

thanks to @michael homer for getting me on the right track

this is the generic script you're looking for. The only downside is your commands are in quotes which means syntax highlighting via your IDE will not really work.

#!/usr/bin/env bash

set -m # allow for job control
EXIT_CODE=0;  # exit code of overall script

function foo() {
   echo "CHLD exit code is $?"
   if [[ $? > 0 ]]; then
      echo "at least one test failed";
      EXIT_CODE=1;
   fi
}

trap 'foo' CHLD

DIRN=$(dirname "$0");

commands=(
    "echo 'x'; exit 1;"
    "echo 'y'; exit 0;"
    "echo 'z'; exit 2;"
)

clen=`expr "${#commands[@]}" - 1` # get length of commands - 1

for i in `seq 0 "$clen"`; do
    (echo "${commands[$i]}" | bash) &   # run the command via bash in subshell
    echo "$i ith command has been issued as a background job"
done

# wait for all to finish
wait;

echo "EXIT_CODE => $EXIT_CODE"
exit "$EXIT_CODE"

# end

thanks to @michael homer for getting me on the right track

added 16 characters in body
Source Link
Alexander Mills
  • 10.9k
  • 27
  • 120
  • 214

this is the generic script you're looking for. The only downside is your commands are in quotes which means syntax highlighting via your IDE will not really work.

    #!/usr/bin/env bash
    
    arr=()  # holds PIDs
    
    commands=(
        "echo 'hallelujah';'x'; exit 1;"
        "echo 'marzipan';'y'; exit 2;"
        "echo 'z'; exit 0;"
    )
    
    commands_len="${#commands[@]}"  # get length of commands
    clen=`expr ${commands_len} - 1`  # subtract one from length
    
    
    for i in `seq 0 "$clen"`
    do
       # run the command via bash in subshell and push PID to array
       (echo "${commands[$i]}" | bash) & arr+=($!)  
    done
    
    len="${#arr[@]}"  # get length of arr
    
    
    EXIT_CODE=0;  # exit code of overall script
    
    for i in `seq 0 "$len"`
    do
        pid="${arr[$i]}";
        wait ${pid} ; CODE="$?"
        if [[ ${CODE} > 0 ]]; then
           echo "at least one test failed";
          EXIT_CODE=1;
          fi
    done
    
    echo "EXIT_CODE => $EXIT_CODE"
    exit "$EXIT_CODE"

    # end

thanks to @michael homer for getting me on the right track

this is the generic script you're looking for. The only downside is your commands are in quotes which means syntax highlighting via your IDE will not really work.

    #!/usr/bin/env bash
    
    arr=()  # holds PIDs
    
    commands=(
        "echo 'hallelujah'; exit 1;"
        "echo 'marzipan'; exit 2;"
    )
    
    commands_len="${#commands[@]}"  # get length of commands
    clen=`expr ${commands_len} - 1`  # subtract one from length
    
    
    for i in `seq 0 "$clen"`
    do
       # run the command via bash in subshell and push PID to array
       (echo "${commands[$i]}" | bash) & arr+=($!)  
    done
    
    len="${#arr[@]}"  # get length of arr
    
    
    EXIT_CODE=0;  # exit code of overall script
    
    for i in `seq 0 "$len"`
    do
        pid="${arr[$i]}";
        wait ${pid} ; CODE="$?"
        if [[ ${CODE} > 0 ]]; then
           echo "at least one test failed";
          EXIT_CODE=1;
          fi
    done
    
    echo "EXIT_CODE => $EXIT_CODE"
    exit "$EXIT_CODE"

    # end

thanks to @michael homer for getting me on the right track

this is the generic script you're looking for. The only downside is your commands are in quotes which means syntax highlighting via your IDE will not really work.

    #!/usr/bin/env bash
    
    arr=()  # holds PIDs
    
    commands=(
        "echo 'x'; exit 1;"
        "echo 'y'; exit 2;"
        "echo 'z'; exit 0;"
    )
    
    commands_len="${#commands[@]}"  # get length of commands
    clen=`expr ${commands_len} - 1`  # subtract one from length
    
    
    for i in `seq 0 "$clen"`
    do
       # run the command via bash in subshell and push PID to array
       (echo "${commands[$i]}" | bash) & arr+=($!)  
    done
    
    len="${#arr[@]}"  # get length of arr
    
    
    EXIT_CODE=0;  # exit code of overall script
    
    for i in `seq 0 "$len"`
    do
        pid="${arr[$i]}";
        wait ${pid} ; CODE="$?"
        if [[ ${CODE} > 0 ]]; then
           echo "at least one test failed";
          EXIT_CODE=1;
          fi
    done
    
    echo "EXIT_CODE => $EXIT_CODE"
    exit "$EXIT_CODE"

    # end

thanks to @michael homer for getting me on the right track

deleted 70 characters in body
Source Link
Alexander Mills
  • 10.9k
  • 27
  • 120
  • 214
Loading
Source Link
Alexander Mills
  • 10.9k
  • 27
  • 120
  • 214
Loading