Created
November 11, 2014 19:29
-
-
Save lavoiesl/43f3f49a59f8d3c0fbc3 to your computer and use it in GitHub Desktop.
BASH utility functions collection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## | |
# 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}$" | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## | |
# 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" ]] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## | |
# 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)" ] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## | |
# 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}" | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## | |
# 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 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## | |
# 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 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## | |
# 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 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## | |
# 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 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## | |
# 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