wiki.bash-hackers.org/howto/collapsing_functions.markup

89 lines
2.9 KiB
Plaintext
Raw Normal View History

2023-04-15 13:23:49 +02:00
====== Collapsing Functions ======
2023-04-15 13:53:05 +02:00
{{keywords>bash shell scripting example function collapse}}
2023-04-15 13:23:49 +02:00
===== What is a "Collapsing Function"? =====
A collapsing function is a function whose behavior changes depending upon the circumstances under which it's run. Function collapsing is useful when you find yourself repeatedly checking a variable whose value never changes.
===== How do I make a function collapse? =====
Function collapsing requires some static feature in the environment. A common example is a script that gives the user the option of having "verbose" output.
2023-04-15 13:53:05 +02:00
<code>
2023-04-15 13:23:49 +02:00
#!/bin/bash
2023-04-15 13:53:05 +02:00
[[ $1 = -v || $1 = --verbose ]] && verbose=1
2023-04-15 13:23:49 +02:00
chatter() {
if [[ $verbose ]]; then
chatter() {
echo &quot;$@&quot;
}
chatter &quot;$@&quot;
else
chatter() {
:
}
fi
}
echo &quot;Waiting for 10 seconds.&quot;
for i in {1..10}; do
chatter &quot;$i&quot;
sleep 1
done
2023-04-15 13:53:05 +02:00
</code>
2023-04-15 13:23:49 +02:00
===== How does it work? =====
The first time you run chatter(), the function redefines itself based on the value of verbose. Thereafter, chatter doesn't check $verbose, it simply is. Further calls to the function reflect its collapsed nature. If verbose is unset, chatter will echo nothing, with no extra effort from the developer.
===== More examples =====
FIXME Add more examples!
2023-04-15 13:53:05 +02:00
<code>
2023-04-15 13:23:49 +02:00
# Somewhat more portable find -executable
# FIXME/UNTESTED (I don't have access to all of the different versions of find.)
# Usage: find PATH ARGS -- use find like normal, except use -executable instead of
# various versions of -perm /+ blah blah and hacks
find() {
hash find || { echo 'find not found!'; exit 1; }
# We can be pretty sure &quot;$0&quot; should be executable.
2023-04-15 13:53:05 +02:00
if [[ $(command find &quot;$0&quot; -executable 2> /dev/null) ]]; then
2023-04-15 13:23:49 +02:00
unset -f find # We can just use the command find
2023-04-15 13:53:05 +02:00
elif [[ $(command find &quot;$0&quot; -perm /u+x 2> /dev/null) ]]; then
2023-04-15 13:23:49 +02:00
find() {
typeset arg args
for arg do
2023-04-15 13:53:05 +02:00
[[ $arg = -executable ]] && args+=(-perm /u+x) || args+=(&quot;$arg&quot;)
2023-04-15 13:23:49 +02:00
done
command find &quot;${args[@]}&quot;
}
2023-04-15 13:53:05 +02:00
elif [[ $(command find &quot;$0&quot; -perm +u+x 2> /dev/null) ]]; then
2023-04-15 13:23:49 +02:00
find() {
typeset arg args
for arg do
2023-04-15 13:53:05 +02:00
[[ $arg = -executable ]] && args+=(-perm +u+x) || args+=(&quot;$arg&quot;)
2023-04-15 13:23:49 +02:00
done
command find &quot;${args[@]}&quot;
}
else # Last resort
find() {
typeset arg args
for arg do
2023-04-15 13:53:05 +02:00
[[ $arg = -executable ]] && args+=(-exec test -x {} \; -print) || args+=(&quot;$arg&quot;)
2023-04-15 13:23:49 +02:00
done
command find &quot;${args[@]}&quot;
}
fi
find &quot;$@&quot;
}
2023-04-15 13:53:05 +02:00
</code>
2023-04-15 13:23:49 +02:00
2023-04-15 13:53:05 +02:00
<code>
2023-04-15 13:23:49 +02:00
#!/bin/bash
# Using collapsing functions to turn debug messages on/off
2023-04-15 13:53:05 +02:00
[ &quot;--debug&quot; = &quot;$1&quot; ] && dbg=echo || dbg=:
2023-04-15 13:23:49 +02:00
# From now on if you use $dbg instead of echo, you can select if messages will be shown
$dbg &quot;This message will only be displayed if --debug is specified at the command line