On Fri, 8 Jul 2022 13:22:46 +0300 Oğuz <oguzismailuy...@gmail.com> wrote:
> 8 Temmuz 2022 Cuma tarihinde Yair Lenga <yair.le...@gmail.com> yazdı: > > > > Practical Example - real life. A job has to copy 3 critical data files. It > > then sends notification via email (non-critical). > > > > #! /bin/bash > > set -o errfail > > function copy-files { > > # ALL files are critical, script will not cont > > cp /new/file1 /production/path/ > > cp /new/file2 /production/path/ > > # Use the files for something - e.g., count the lines. > > important-job /production/path/file1 /production/path/file2 > > > > ls -l /production/path | mail -s "all-good" not...@company.com || > > true # Not critical > > } > > > > if copy-files ; then > > more-critical-jobs > > echo "ALL GOOD" > > else > > mail -s "PROBLEM" nor...@company.com < /dev/null > > fi > > > > What is the difference ? consider the case where /new/file1 does not > > exists, which is critical error. > > * Without errfail, an error message will be sent to script stderr, but the > > script will continue to copy the 2nd file, and to perform the > > important-job, even though the data is not ready. > > > How is this any better than doing `cp ... && cp ... && important-job ...'? Just to elaborate on the premise of the question, rather than address the question itself, the sample could have been written as: #!/bin/bash copy_files() { cp /new/file1 /production/path/ && cp /new/file2 /production/path/ && important-job /production/path/file1 /production/path/file2 } if copy_files; then ls -l /production/path | mail -s "all-good" not...@company.com echo "ALL GOOD" else mail -s "PROBLEM" not...@company.com < /dev/null false # not in the original but one probably wanted either this or exit 1 fi Alternatively, if it is considered necessary to issue ls from within the function: copy_files() { cp /new/file1 /production/path/ && cp /new/file2 /production/path/ && important-job /production/path/file1 /production/path/file2 || return ls -l /production/path | mail -s "all-good" not...@company.com true } Ultimately, which approach is easier to reason with? Consider it as an open question. -- Kerin Millar