Skip to content

Instantly share code, notes, and snippets.

@lavoiesl
Created November 11, 2014 19:29
Show Gist options
  • Save lavoiesl/43f3f49a59f8d3c0fbc3 to your computer and use it in GitHub Desktop.
Save lavoiesl/43f3f49a59f8d3c0fbc3 to your computer and use it in GitHub Desktop.
BASH utility functions collection
##
# Determine if a brew is installed.
#
# @uses brew
# @param string brew name
# @returns success code
#
function brew_installed() {
local brew="${1}"
brew list -1 | grep -q "^${brew}$"
}
##
# Prompt the user to confirm, default is yes
#
# @uses read
# @param string question
# @returns 0 if yes, 1 if no
#
function confirm() {
local prompt="${1}"
read -p "${prompt} (Y/n) " reply
[[ "${reply}" != "n" ]]
}
##
# Check if directory contains executable files
#
# @link http://stackoverflow.com/questions/4458120/unix-find-search-for-executable-files
# @uses find
# @returns 0 if contains, 1 if empty
#
function dir_has_executables() {
local dir="${1}"
[ -n "$(find -L "${dir}" -type f -perm '+111' -mindepth 1 -maxdepth 1)" ]
}
##
# Escape custom characters in a string
# WARNING: if \ is escaped, it must be the first
# Example: escape "ab'\c" '\' "'" ===> ab\'\\c
#
# @param string The string to be escaped
# @param char* All the characters to espace, multiple chars are specified as multiple params.
# @outputs Escaped string
#
function escape_chars() {
local content="$1"
shift
for char in $@; do
if [ "${char}" = '$' -o "${char}" = '\' ]; then
char="\\${char}"
fi
content="$(echo "${content}" | sed -e "s/${char}/\\\\${char}/g")"
done
echo "${content}"
}
##
# Determine if a program is in path, without any output.
#
# @uses which
# @param string executable name
# @returns success code
#
function program_exists() {
local program="$1"
[ -n "${program}" ] && which "${program}" >/dev/null
}
##
# Prompt the user with a question, supporting default value
#
# @uses read
# @param string question
# @param string optional default value
# @outputs answer or default value
#
function prompt_value() {
local prompt="${1} "
local default="${2}"
[[ -n "${default}" ]] && prompt="${prompt}(${default}) "
read -p "${prompt}" reply
[[ -z "${reply}" ]] && reply="${default}"
if [[ -z "${reply}" ]]; then
prompt_value "${prompt}" "${default}"
else
echo "${reply}"
fi
}
##
# Convert to fully qualified path, expanding ../, symlinks, etc.
# Readlink of Mac is deficient, use ruby instead.
#
# @uses ruby
# @param string any path (can be . and such)
# @outputs absolute path
#
function realpath() {
local path="$1"
echo "require 'pathname'; puts Pathname.new('${path}').realpath" | ruby 2>/dev/null
}
##
# Print all passed arguments in reverse
#
# @param string* All the arguments to be printed
# @outputs One argument per line
#
function reverse_args() {
local items=($@)
for (( idx=${#items[@]}-1 ; idx>=0 ; idx-- )) ; do
echo "${items[idx]}"
done
}
##
# Sort all files by their filename, independently of their folder
# Example: ls -1 /a/* /b/* | sort_by_filename
#
# @input One path per line
# @outputs One path per line
function sort_by_filename() {
while read filename; do
echo "${filename}" | sed -E 's/^(.+)\/([^/]+)$/\2#\1\/\2/';
done | sort -n | cut -d '#' -f 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment