-
Notifications
You must be signed in to change notification settings - Fork 0
/
try
executable file
·106 lines (88 loc) · 3.1 KB
/
try
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#!/usr/bin/env bash
#
# #############################################################
# Project: shell (none)
# File...: lib/try
# Created: <unknown>, 2014/08/07 - 10:30:00
# Author.: Mathias Henze, user:3917943 (stackoverflow)
# ~·~·~·~·~·~·~·~·~·~·~·~·~~·~·~·~·~·~·~·~·~·~·~·~·~~·~·~·~·~·~~·~·~·~·~·~·~·~
# Last Modified: Thursday, 2023/01/05 - 00:58:39
# Modified By..: @fbnmtz, ([email protected])
# ~·~·~·~·~·~·~·~·~·~·~·~·~~·~·~·~·~·~·~·~·~·~·~·~·~~·~·~·~·~·~~·~·~·~·~·~·~·~
# Version: 0.1.2.14
# ~·~·~·~·~·~·~·~·~·~·~·~·~~·~·~·~·~·~·~·~·~·~·~·~·~~·~·~·~·~·~~·~·~·~·~·~·~·~
# Description:
# > https://stackoverflow.com/questions/22009364/is-there-a-try-catch-command-in-bash
# ############################################################################
# HISTORY:
#
# #############################################################
#
_xLIB_TRY_=true;
export AnException=100
export AnotherException=101
function try(){
[[ $- = *e* ]]; SAVED_OPT_E=$?
set +e
}
function throw(){
exit $1
}
function catch(){
export ex_code=$?
(( $SAVED_OPT_E )) && set +e
return $ex_code
}
function throwErrors(){
set -e
}
function ignoreErrors(){
set +e
}
# ~·~·~·~·~·~·~·~·~·~·~·~·~~·~·~·~·~·~·~·~·~·~·~·~·~~·~·~·~·~·~~·~·~·~·~·~·~·~
# USAGE
# ~·~·~·~·~·~·~·~·~·~·~·~·~~·~·~·~·~·~·~·~·~·~·~·~·~~·~·~·~·~·~~·~·~·~·~·~·~·~
# # start with a try
# try
# ( # open a subshell !!!
# echo "do something"
# [ someErrorCondition ] && throw $AnException
# echo "do something more"
# executeCommandThatMightFail || throw $AnotherException
# throwErrors # automaticatly end the try block, if command-result is non-null
# echo "now on to something completely different"
# executeCommandThatMightFail
# echo "it's a wonder we came so far"
# executeCommandThatFailsForSure || true # ignore a single failing command
# ignoreErrors # ignore failures of commands until further notice
# executeCommand1ThatFailsForSure
# local result = $(executeCommand2ThatFailsForSure)
# [ result != "expected error" ] && throw $AnException # ok, if it's not an expected error, we want to bail out!
# executeCommand3ThatFailsForSure
# # make sure to clear $ex_code, otherwise catch * will run
# # echo "finished" does the trick for this example
# echo "finished"
# )
# # directly after closing the subshell you need to connect a group to the catch using ||
# catch || {
# # now you can handle
# case $ex_code in
# $AnException)
# echo "AnException was thrown"
# ;;
# $AnotherException)
# echo "AnotherException was thrown"
# ;;
# *)
# echo "An unexpected exception was thrown"
# throw $ex_code # you can rethrow the "exception" causing the script to exit if not caught
# ;;
# esac
# }
# another way
# { # try
# command1 &&
# #save your output
# } || { # catch
# # save log for exception
# }