简体   繁体   中英

Send mailx if all 8 scripts apply correctly

I've got a script (main one) that calls 8 others. I need to send a mail if all of these return the expected results (inside each of them I can add a variable that takes 1 if it went correctly or 0 if it goes wrong). But I don't have a clue on how to check those 8 variables and if everyone is 1 (correct) send the mail (I do know how to configurate the mail)

any hint will be helpfull I can't seem to drop an idea.

One way is to chain the commands using && :

command1 &&
    command2 &&
    command3 &&
    command4 &&
    command5 &&
    command6 &&
    command7 &&
    command8 &&
    sendmail command here

This assumes each command exits with status 0 (not 1 ) on success, which is the usual Unix / Posix way for these things to be arranged.

To be clear, x && y runs x and then runs y , but y is only run if x succeeded (exited with 0 ).

As he mentions, Danfuzz' suggestion will only run all the subsidiary commands as long as each one returns the expected results. If you want to run all of them no matter what, you can do something like:

command_1
result_1=$?
command_2
result_2=$?
command_3
result_3=$?
command_4
result_4=$?

if [ ${result_1} = 0 -a ${result_2} = 0 -a ${result_3} = 0 -a ${result_4} = 0 ]; then
    echo "ALL ARE OK"
else
    echo "AT LEAST ONE IS NOT OK"
fi

In order for this to work, the commands have to exit 0 on success and use a different exit value on failure, as Danfuzz suggests.

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