Похоже, вы хотите выполнить $1
и в зависимости от успеха или неудачи выполнить $2
или $3
. Вот один из способов сделать это:
successhandler() {
echo GREAT SUCCESS
}
failurehandler() {
echo sad failure
}
checkcondition() {
if (( RANDOM < 15000 ))
then
true
else
false
fi
}
myif() {
# disable filename generation (in case globs are present)
set -f
if $1 > /dev/null 2>&1
then
$2
true
else
$3
false
fi
}
Здесь я создал произвольные версии обработчиков успеха, обработчиков ошибок и контрольных условий, чтобы продемонстрировать поведение.
Вот несколько примеров запуска:
$ myif true successhandler failurehandler
GREAT SUCCESS
$ myif false successhandler failurehandler
sad failure
$ myif 'test -f /etc/hosts' successhandler failurehandler
GREAT SUCCESS
$ myif 'test -f /etc/hosts/not/there' successhandler failurehandler
sad failure
$ myif checkcondition successhandler failurehandler
GREAT SUCCESS
$ myif checkcondition successhandler failurehandler
sad failure
$ myif checkcondition successhandler failurehandler
GREAT SUCCESS
$ myif checkcondition successhandler failurehandler
sad failure
$ myif checkcondition successhandler failurehandler
sad failure
Внутри myif()
я специально опускаю stdout и stderr в /dev/null
; отрегулируйте это по своему усмотрению.
Таким образом, решение состояло в том, чтобы преобразовать мою установку Windows в UEFI (с помощью mbr2gpt в Windows ), после этого мне пришлось снова установить grub по умолчанию, и все работает нормально.