This is a Bash shell library that provides a set of common utility functions to ease and streamline the scripting process in Debian-like environments.
Initializes color variables for terminal text formatting.
Parameters: None
Example:
set_colors
echo -e "${GREEN}Success!${NC}"Available colors: GREEN, RED, YELLOW, BLUE, NC (No Color), BOLD, UNDERLINE
Creates a timestamped backup of a file if it exists.
Parameters:
$1- The path to the file to backup
Example:
backup_file "/etc/config.conf"
# Creates: /etc/config.conf.bak.20240206123045Checks if the first parameter is 'silent' to determine if output should be suppressed.
Parameters:
$1- (Optional) Pass "silent" to return true
Returns: 0 if silent, 1 otherwise
Example:
if is_silent "$1"; then
echo "Running in silent mode"
fiVerifies that the script is running on a Linux distribution.
Parameters: None
Exits: With code 1 if not running on Linux
Verifies that the system is running a 64-bit operating system.
Parameters: None
Exits: With code 1 if not 64-bit
Checks if running on a Raspberry Pi and verifies minimum model requirement.
Parameters:
$1- The minimal Raspberry Pi model number required
Example:
check_rpi_model 4 # Requires Raspberry Pi 4 or higherVerifies that the script is running with the correct user privileges.
Parameters:
$1- Either "privileged" (must be root) or "regular" (must not be root)
Example:
check_user_privileges "privileged" # Requires root
check_user_privileges "regular" # Requires non-rootVerifies that the 'apt' package manager is available.
Parameters: None
Exits: With code 1 if apt is not installed
Updates the operating system using apt package manager.
Parameters:
$1- (Optional) Pass "silent" to suppress output
Example:
update_os # Verbose mode
update_os silent # Silent modeInstalls one or more packages using apt package manager.
Parameters:
$1- (Optional) Pass "silent" as first argument to suppress output$@- Package names to install
Example:
install_packages nginx mysql-server php
install_packages silent nginx mysql-server phpSets the system timezone.
Parameters:
$1- Valid timezone (e.g., "Europe/Amsterdam")
Example:
set_timezone "Europe/Amsterdam"
set_timezone "America/New_York"Checks for required tools and exits if any are missing.
Parameters:
$@- Names of commands/tools to check for
Example:
require_tool git curl wgetValidates input based on a specified type.
Parameters:
$1- The input value to validate$2- The type of the variable: "y/n", "num", "str", "email", or "host"$3- The name of the variable (used for error messages)
Returns: 0 if the input is valid, 1 otherwise.
Example:
if validate_input "yes" "y/n" "CONFIRMATION"; then
echo "Valid input"
else
echo "Invalid input"
fiPrompts the user for input with validation and default values. The function follows two paths:
-
Environment Variable Path: If an environment variable with the same name as the variable is set, its value is used directly. The value is validated, and if it is invalid, the script exits immediately with an error message.
-
User Input Path: If no environment variable is set, the user is prompted for input. The function re-asks the question until valid input is provided.
This ensures that the script can operate non-interactively when environment variables are pre-set, while still enforcing validation.
Parameters:
$1- Variable name to store the result$2- Default value if user presses Enter$3- Prompt message to display$4- (Optional) Validation type: "y/n", "num", "str", "email", or "host" (default: "str")
Example:
ask_user "USERNAME" "admin" "Enter username" "str"
ask_user "PORT" "8080" "Enter port number" "num"
ask_user "ENABLE_SSL" "y" "Enable SSL?" "y/n"
ask_user "EMAIL" "admin@example.com" "Enter email" "email"
ask_user "HOSTNAME" "server.local" "Enter hostname" "host"Downloads files using curl with error handling and optional backup.
Single file mode:
download_file URL DEST DESCRIPTION [backup]Multi-file mode:
download_file -m DEST_DIR DESCRIPTION [backup] URL1:FILENAME1 URL2:FILENAME2Examples:
# Single file
download_file "http://example.com/config.txt" "/etc/app/config.txt" "config file"
# Single file with backup
download_file "http://example.com/config.txt" "/etc/app/config.txt" "config file" backup
# Multiple files
download_file -m "/opt/app" "library files" \
"http://example.com/lib1.so:lib1.so" \
"http://example.com/lib2.so:lib2.so"
# Multiple files with backup
download_file -m "/opt/app" "library files" backup \
"http://example.com/lib1.so:lib1.so" \
"http://example.com/lib2.so:lib2.so"These functions can be imported and used in your own Bash scripts. To import the functions, use the following lines in your script:
# Remove old functions libraries and download the latest version
rm -f /tmp/functions.sh
if ! curl -s -o /tmp/functions.sh https://raw.githubusercontent.com/oszuidwest/bash-functions/main/common-functions.sh; then
echo -e "*** Failed to download functions library. Please check your network connection! ***"
exit 1
fi
# Source the functions file
source /tmp/functions.shAnd use it like this:
# Example
set_colors
echo -e "${GREEN}This text is green!${NC}"To check for required tools:
require_tool git curl wgetOr this to ask the user for input, set a default (n) and validate the input (it should be y or n)
ask_user "SSL" "n" "Do you want Let's Encrypt to get a certificate for this server? (y/n)" "y/n"All functions are documented inline with their parameters.
This script should be run on a Unix-like system, such as Linux, that uses the Bash shell and 'apt' package manager. Certain functions specifically check for these conditions and will exit if they are not met.
Always ensure to use the correct permissions when running scripts that import these functions. For instance, functions like update_os and install_packages should be run with root permissions.
Be aware that the ask_user function does not just prompt the user for input, but also assigns the input to a variable. The name of the variable is passed as the first argument, and the function uses the eval command to assign the input to it. It's important to only pass safe, pre-defined strings as the first argument.
This project is licensed under the MIT License - see the LICENSE.md file for details.
Bugs, feedback, and ideas are welcome at techniek@zuidwesttv.nl or through pull requests.