Как я могу запустить функцию как "проверенную команду" и выполнить действие при сбое, все еще прерывая функцию, как только возникает ошибка?Рассмотрим следующий скрипт:
#!/bin/bash -e
function foo() {
echo Entering foo
false
echo Should not reach this line
}
foo || echo I want to see this line on failure in foo
foo
Вывод, который я получаю:
Entering foo
Should not reach this line
Entering foo
Хотя я хотел бы получить
Entering foo
I want to see this line on failure in foo
Entering foo
Я думаю, что япоиск - это способ пометить функцию как непроверенную команду.Согласно man-странице bash
-e errexit
Exit immediately if any untested command fails in non-interactive
mode. The exit status of a command is considered to be explicitly
tested if the command is part of the list used to control an if,
elif, while, or until; if the command is the left hand operand of
an “&&” or “||” operator; or if the command is a pipeline preceded
by the ! operator. If a shell function is executed and its exit
status is explicitly tested, all commands of the function are con‐
sidered to be tested as well.
EDIT Ожидаемый результат был неверным.отредактировал это для ясности