BASIS  version 1.2.3 (revision 2104)
stdaux.sh
Go to the documentation of this file.
00001 ##############################################################################
00002 # @file  stdaux.sh
00003 # @brief Standard auxiliary functions for BASH.
00004 #
00005 # @note The stdaux.sh module is automatically created by BASIS from the
00006 #       template file stdaux.sh.in which is part of the BASIS installation.
00007 #
00008 # Copyright (c) 2011 University of Pennsylvania. All rights reserved.<br />
00009 # See https://www.cbica.upenn.edu/sbia/software/license.html or COPYING file.
00010 #
00011 # Contact: SBIA Group <sbia-software at uphs.upenn.edu>
00012 ##############################################################################
00013 
00014 # return if already loaded
00015 [ "${_SBIA_STDAUX_STDAUX_INCLUDED:-0}" -eq 1 ] && return 0
00016 _SBIA_STDAUX_STDAUX_INCLUDED=1
00017 
00018 
00019 ## @addtogroup BasisBashUtilities
00020 #  @{
00021 
00022 
00023 # ============================================================================
00024 # version / contact
00025 # ============================================================================
00026 
00027 # ----------------------------------------------------------------------------
00028 ## @brief Print version information.
00029 #
00030 # @param [in] name      Name of program. Give a constant expression here,
00031 #                       @b not the name of the executable as extracted from
00032 #                       the first command-line argument or similar! By default,
00033 #                       however, the executable name is extracted from the first
00034 #                       command-line argument. This is @b not recommended.
00035 # @param [in] copyright Copyright and license notice. Defaults to official
00036 #                       SBIA software license.
00037 #
00038 # @returns Nothing.
00039 function print_version
00040 {
00041     local name=${1:-}
00042     local copyright=${2:-}
00043     if [ -z "${name}" ]; then
00044         echo "print_version(): Executable name not specified!" 1>&2
00045         exit 1
00046     fi
00047     if [ -z "${copyright}" ]; then
00048         copyright="Copyright (c) University of Pennsylvania. All rights reserved.\n\
00049 See https://www.cbica.upenn.edu/sbia/software/license.html or COPYING file."
00050     fi
00051     echo "${name} (BASIS) version 1.2.3 (revision 2104)"
00052     echo -e "${copyright}"
00053 }
00054 
00055 # ----------------------------------------------------------------------------
00056 ## @brief Print contact information.
00057 #
00058 # @param [in] contact Contact name. Defaults to official software contact.
00059 #                     Generally, it is @b not advised to use a contact different
00060 #                     from the official one.
00061 #
00062 # @returns Nothing.
00063 function print_contact
00064 {
00065     echo "Contact:"
00066     if [ $# -gt 0 ]; then
00067         echo -e "  $1"
00068     else
00069         echo "  SBIA Group <sbia-software at uphs.upenn.edu>"
00070     fi
00071 }
00072 
00073 # ============================================================================
00074 # command execution
00075 # ============================================================================
00076 
00077 # ----------------------------------------------------------------------------
00078 ## @brief Execute command as subprocess.
00079 #
00080 # This function is used to execute a subprocess within a BASH script.
00081 #
00082 # Example:
00083 # @code
00084 # # the next command will exit the current shell if it fails
00085 # execute_process ls /not/existing
00086 # # to prevent this, provide the --allow_fail option
00087 # execute_process --allow_fail ls /not/existing
00088 # # to make it explicit where the command-line to execute starts, use --
00089 # execute_process --allow_fail -- ls /not/existing
00090 # @endcode
00091 #
00092 # Note that the output of the command is not redirected by this function.
00093 # In order to execute the command quietly, use this function as follows:
00094 # @code
00095 # execute_process ls / &> /dev/null
00096 # @endcode
00097 # Or to store the command output in a variable including error messages
00098 # use it as follows:
00099 # @code
00100 # output=`execute_process ls / 2>&1`
00101 # @endcode
00102 # Note that in this case, the option --allow_fail has no effect as the
00103 # calling shell will never be terminated. Only the subshell in which the
00104 # command is executed will be terminated. Checking the exit code $? is
00105 # in this case required.
00106 #
00107 # @param [in] options Function options as documented below.
00108 # @param [in] cmd     Executable of command to run or corresponding build
00109 #                     target name. This is assumed to be the first
00110 #                     non-option argument or the argument that follows the
00111 #                     special '--' argument.
00112 # @param [in] args    All remaining arguments are passed as arguments to
00113 #                     the given command.
00114 # @par Options:
00115 # <table border="0">
00116 #   <tr>
00117 #     @tp @b --allow_fail @endtp
00118 #     <td>Allows the command to fail. By default, if the command
00119 #         returns a non-zero exit code, the exit() function is
00120 #         called to terminate the current shell.</td>
00121 #   </tr>
00122 #   <tr>
00123 #     @tp <b>--verbose, -v</b> @endtp
00124 #     <td>Print command-line to stdout before execution.</td>
00125 #   </tr>
00126 #   <tr>
00127 #     @tp @b --simulate @endtp
00128 #     <td>If this option is given, the command is not actually
00129 #         executed, but the command-line printed to stdout only.</td>
00130 #   </tr>
00131 # </table>
00132 #
00133 # @returns Exit code of subprocess.
00134 function execute_process
00135 {
00136     # parse arguments
00137     local allow_fail='false'
00138     local simulate='false'
00139     local verbose=0
00140     while [ $# -gt 0 ]; do
00141         case "$1" in
00142             --allow_fail)
00143                 allow_fail='true'
00144                 ;;
00145             --verbose|-v)
00146                 ((verbose++))
00147                 ;;
00148             --simulate)
00149                 simulate='true'
00150                 ;;
00151             --)
00152                 shift
00153                 break
00154                 ;;
00155             *)
00156                 break
00157                 ;;
00158         esac
00159         shift
00160     done
00161     # command to execute and its arguments
00162     local command="$1"; shift
00163     [ -n "${command}" ] || return 1
00164     # map build target name to file
00165     local exec_path && get_executable_path exec_path "${command}"
00166     if [ -z "${exec_path}" ]; then
00167         echo "${command}: Either unknown build target or command not found" 1>&2
00168         exit 1
00169     fi
00170     # some verbose output
00171     if [ ${verbose} -gt 0 ]; then
00172         local argsstr && basis_array_to_quoted_string args "$@"
00173         echo "\$ ${exec_path} ${argsstr}"
00174     fi
00175     # execute command
00176     [ "X${simulate}" == "Xtrue" ] || {
00177         "${exec_path}" "$@"
00178     }
00179     local status=$?
00180     # if command failed, exit
00181     [ ${status} -eq 0 -o "X${allow_fail}" == "Xtrue" ] || {
00182         echo "Command ${exec_path} ${argsstr} failed" 1>&2
00183         exit 1
00184     }
00185     # return exit code
00186     return ${status}
00187 }
00188 
00189 
00190 ## @}
00191 # end of Doxygen group