Skip to content

Commit

Permalink
Adding missing shell functions
Browse files Browse the repository at this point in the history
  • Loading branch information
AlanH42 committed Aug 13, 2011
1 parent 03ad088 commit 70e7dd9
Showing 1 changed file with 109 additions and 0 deletions.
109 changes: 109 additions & 0 deletions scripts/md5
Original file line number Diff line number Diff line change
@@ -1,5 +1,114 @@
#!/usr/bin/env bash

#
# ## variable_is_nonempty()
#
# Test to see if a variable is empty.
#
# ### Input Parameters
#
# First parameter is a string containing a variable name.
#
# ### Stream Outputs
#
# None.
#
# ### Environmental effects
#
# None.
#
# ### Return Codes
#
# 0 for success.
#
# ### Failure Scenarios
#
# Fails if no variable name was given as the first argument.
#
# ### Usage Examples
#
# user$ variable_is_nonempty asdf
# user$ echo $?
# 1
#
# user$ asdf="w00t! "
# user$ variable_is_nonempty asdf
# user$ echo $?
# 0
#
variable_is_nonempty()
{
# Store the first parameter, which should be the variable name, in the
# variable variable :)
local _variable="${1:-}"

# If the variable name is nonempty
if [[ -n "${_variable}" ]]
then
# If the evaluation of a nonempty test with the variable name used [huh???]
eval "[[ -n \"\${${_variable}:-}\" ]]"
else
# Otherwise no parameters were passed in; this is a programming error.
# Send a failure message which also triggers a backtrace enabling the
# developer to quickly pinpoint and fix their error.
fail "Cannot check if variable is nonempty; no variable was given."
fi

}

#
# ## command\_exists()
#
# Checks to see whether a command exists within the current environment and PATH.
#
# ### Input Parameters
#
# First parameter is a command name.
#
# ### Stream Outputs
#
# None.
#
# ### Environmental effects
#
# none.
#
# ### return codes
#
# 0 if the command was found in the current environment.
# 1 if the command was not found in the current environment.
#
# ### failure scenarios
#
# Fails if no command name was given.
#
# ### usage examples
#
# user$ command_exists adsf
# user$ echo $?
# 1
#
# user$ command_exists ls
# user$ echo $?
# 0
#
command_exists()
{
local _name="${1:-}"

if variable_is_nonempty _name
then
if command -v "${_name}" > /dev/null 2>&1
then
return 0
else
return 1
fi
else
fail "Cannot test if command exists; no command name was given."
fi
}

if (( ${rvm_trace_flag:=0} == 2 ))
then
set -x
Expand Down

0 comments on commit 70e7dd9

Please sign in to comment.