Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add function to print the version of commands #22

Merged
merged 2 commits into from Oct 24, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 17 additions & 0 deletions shtdlib.sh
Expand Up @@ -347,6 +347,23 @@ function compare_versions {
return $(( lowest_ver_line-1 ))
}

# Prints the version of a command, accepts 1-4 parameters
# 1. Full or relative path to command (required)
# 2. Text to display before version info (optional)
# 3. Flag/Argument to command to get version (optional, defaults to --version)
# 4. Error message if command is not found, to ignore redirect stderr like
hrpatel marked this conversation as resolved.
Show resolved Hide resolved
# this: print_version bash 2> /dev/null
function print_version {
local error_msg
error_msg="${4:-Unable to find command ${1}}"
if command -v "${1}" > /dev/null ; then
echo "${2:-}"
${1} "${3:---version}"
else
(>&2 echo "${error_msg}")
fi
}

# Converts relative paths to full paths, ignores invalid paths
# Accepts either the path or name of a variable holding the path
function finalize_path {
Expand Down