| Line | |
|---|
| 1 | # Contributed by Noah Friedman.
|
|---|
| 2 |
|
|---|
| 3 | # To avoid using a function in bash, you can use the `builtin' or
|
|---|
| 4 | # `command' builtins, but neither guarantees that you use an external
|
|---|
| 5 | # program instead of a bash builtin if there's a builtin by that name. So
|
|---|
| 6 | # this function can be used like `command' except that it guarantees the
|
|---|
| 7 | # program is external by first disabling any builtin by that name. After
|
|---|
| 8 | # the command is done executing, the state of the builtin is restored.
|
|---|
| 9 | function external ()
|
|---|
| 10 | {
|
|---|
| 11 | local state=""
|
|---|
| 12 | local exit_status
|
|---|
| 13 |
|
|---|
| 14 | if builtin_p "$1"; then
|
|---|
| 15 | state="builtin"
|
|---|
| 16 | enable -n "$1"
|
|---|
| 17 | fi
|
|---|
| 18 |
|
|---|
| 19 | command "$@"
|
|---|
| 20 | exit_status=$?
|
|---|
| 21 |
|
|---|
| 22 | if [ "$state" = "builtin" ]; then
|
|---|
| 23 | enable "$1"
|
|---|
| 24 | fi
|
|---|
| 25 |
|
|---|
| 26 | return ${exit_status}
|
|---|
| 27 | }
|
|---|
| 28 |
|
|---|
| 29 | # What is does is tell you if a particular keyword is currently enabled as
|
|---|
| 30 | # a shell builtin. It does NOT tell you if invoking that keyword will
|
|---|
| 31 | # necessarily run the builtin. For that, do something like
|
|---|
| 32 | #
|
|---|
| 33 | # test "$(builtin type -type [keyword])" = "builtin"
|
|---|
| 34 | #
|
|---|
| 35 | # Note also, that disabling a builtin with "enable -n" will make builtin_p
|
|---|
| 36 | # return false, since the builtin is no longer available.
|
|---|
| 37 | function builtin_p ()
|
|---|
| 38 | {
|
|---|
| 39 | local word
|
|---|
| 40 |
|
|---|
| 41 | set $(builtin type -all -type "$1")
|
|---|
| 42 |
|
|---|
| 43 | for word in "$@" ; do
|
|---|
| 44 | if [ "${word}" = "builtin" ]; then
|
|---|
| 45 | return 0
|
|---|
| 46 | fi
|
|---|
| 47 | done
|
|---|
| 48 |
|
|---|
| 49 | return 1
|
|---|
| 50 | }
|
|---|
Note:
See
TracBrowser
for help on using the repository browser.