Note that there are some explanatory texts on larger screens.

plurals
  1. POPass a full bash script line to another bash function to execute
    text
    copied!<p>in the BASH code below, the variable ECHO_ALL is a global and set to either 'yes' or 'no' based on input parsing of options.</p> <p>--- begin of ~/scripts/util/util-optout.sh ---</p> <pre><code>######################################## # @param $@ # @return the return value from $@ # @brief A wrapper function to allow # for OPTional OUTput of any # command w/wo args ####################################### optout() { if [ ${ECHO_ALL} = 'no' ]; then "$@" 1&gt;/dev/null 2&gt;&amp;1 return $? else "$@" return $? fi } </code></pre> <p>--- end of file ---</p> <p>in another bash file I source the above util-optout.sh file and use the optout() function to allow for conditional output.. essentially allow for conditional redirection of any commands output to /dev/null to make scripts silent.</p> <p>for example in some other build script i have</p> <pre><code>source ~/scripts/util/util-optout.sh optout pushd ${ZLIB_DIR} optout rm -vf config.cache optout CC=${BUILD_TOOL_CC} ./configure ${ZLIB_CONFIGURE_OPT} --prefix=${CURR_DIR}/${INSTALL_DIR} # ^^^^^^^^^^^^^^^^^^^ # ^ this breaks my optout() command # my optout() fails when there are prefixed bash env vars set like CC=${...} before ./configure optout popd optout make -C ${ZLIB_DIR} ${ZLIB_COMPILER_OPT} all optout make -C ${ZLIB_DIR} install </code></pre> <p>for simple commands with any type of parameters after it like 'pushd' or 'rm'.. optout() works great. even the optout make -C ones work fine.</p> <p>but it gives me an error for commands that have prefix env-vars set like the optout CC=${...} ./configure ... </p> <p>utils/util-optout.sh: line 33: CC=gcc: command not found</p> <p>Is there a way to make my optout() function work for ANY possible valid bash script line.</p> <p>i know it has something to do with the use of "$@" or "$*" in my optout() function, but i have studied the bash man pages in detail and I can't make it work for all possible bash line cases.</p> <p>so far the only way to get past this limitation with my optout() is the following 3-line style; which is annoying.</p> <pre><code>export CC=${BUILD_TOOL_CC} optout ./configure ${ZLIB_CONFIGURE_OPT} --prefix=${CURR_DIR}/${INSTALL_DIR} unset CC </code></pre> <p>Any ideas on how to reduce it all back down to a single optout ... line</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload