Skip to content

Latest commit

 

History

History
162 lines (126 loc) · 3.88 KB

os.md

File metadata and controls

162 lines (126 loc) · 3.88 KB

Bash-Funk "os" module

The following commands are available when this module is loaded:

  1. -command-exists
  2. -pkg-installed
  3. -test-all-os

License

SPDX-FileCopyrightText: © Vegard IT GmbH (https://vegardit.com)
SPDX-License-Identifier: Apache-2.0

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

-command-exists

Usage: -command-exists [OPTION]... COMMAND

Checks if the given program or function is available.

Parameters:
  COMMAND (required)
      Name of the program or function.

Options:
-v, --verbose
        Prints additional information during command execution.
    -----------------------------
    --help
        Prints this help.
    --tracecmd
        Enables bash debug mode (set -x).
    --selftest
        Performs a self-test.
    --
        Terminates the option list.

Examples:
$ -command-exists hash

$ -command-exists -v hash
'hash' is available.
$ -command-exists -v ls
'ls' is available.
$ -command-exists -v name-of-nonexistant-command
'name-of-nonexistant-command' not found.

Implementation:

if hash "$_COMMAND" &>/dev/null; then
   [[ $_verbose ]] && echo "'${_COMMAND}' is available." || :
   return 0
else
   [[ $_verbose ]] && echo "'${_COMMAND}' not found." || :
   return 1
fi

-pkg-installed

Usage: -pkg-installed [OPTION]... PACKAGE_NAME

Determines if the given software package is installed.

Parameters:
  PACKAGE_NAME (required)
      Name of the package.

Options:
-v, --verbose
        Prints additional information during command execution.
    -----------------------------
    --help
        Prints this help.
    --tracecmd
        Enables bash debug mode (set -x).
    --selftest
        Performs a self-test.
    --
        Terminates the option list.

Implementation:

if hash "yum" &>/dev/null; then
   if yum list installed "${_PACKAGE_NAME}" &>/dev/null; then
      [[ $_verbose ]] && echo "${_PACKAGE_NAME} is installed." || :
      return 0
   fi

elif hash "dpkg-query" &>/dev/null; then
   if dpkg-query -Wf'${Status}' "${_PACKAGE_NAME}" 2>/dev/null | grep "install ok installed" &>/dev/null; then
      [[ $_verbose ]] && echo "${_PACKAGE_NAME} is installed." || :
      return 0
   fi

elif hash "cygcheck" &>/dev/null; then
   if cygcheck "${_PACKAGE_NAME}" &>/dev/null; then
      [[ $_verbose ]] && echo "${_PACKAGE_NAME} is installed." || :
      return 0
   fi

elif hash "rpm" &>/dev/null; then
   if rpm -q openssh &>/dev/null; then
      [[ $_verbose ]] && echo "${_PACKAGE_NAME} is installed." || :
      return 0
   fi

else
   echo "-pkg-installed: Error: Unable to determine installation status of ${_PACKAGE_NAME}. No supported package manager found." || :
   return 2
fi

[[ $_verbose ]] && echo "${_PACKAGE_NAME} is NOT installed." || :
return 1

-test-all-os

Usage: -test-all-os [OPTION]...

Performs a selftest of all functions of this module by executing each function with option '--selftest'.

Options:
    --help
        Prints this help.
    --tracecmd
        Enables bash debug mode (set -x).
    --selftest
        Performs a self-test.
    --
        Terminates the option list.

Implementation:

-command-exists --selftest && echo || return 1
-pkg-installed --selftest && echo || return 1