when bash scripts start getting complex, break them functions. applies complex pipes, sequence of complicated pipe commands (e.g. containing while-loops) can become hard read. more when parallelization wanted, xargs helpful.
i know can export functions subshell export -f, in simple case can do
export -f myfunction some-command | xargs -iline bash -c "myfunction 'line'" but if myfunction depends on other functions becomes hard maintain -- every time function changes such functions needed subshell executing myfunction change, export statement have changed -- seems pretty error prone.
is there general way export functions use subshells? thinking along lines of "export defined functions" command, allow code structure like
main() { ... } func1 () { ... } func2 () { ... } <export functions> main "$@"
your question asks exporting functions. easy in bash, see below.
your question title/subject implies using functions in xargs, though script; don't know xargs can "call" bash function directly, can of course wrap use of exported function(s) in script called xargs, see below.
first, function list functions. user functions default , -v list functions:
lsfns () { case "$1" in -v | v*) # verbose: set | grep '()' --color=always ;; *)⋅ declare -f | cut -d" " -f3 | egrep -v "^_" ;; esac } next function export user functions:
exportfns () { export -f $(lsfns); } or put export -f $(lsfns) in .bashrc.
example script doit.sh:
#!/bin/bash lsfns "$@" # make use of function exported parent shell :) example command line (after chmod a+rx doit.sh):
echo -v | xargs doit.sh compare with
echo "" | xargs doit.sh enjoy
zenaan
Comments
Post a Comment