{"id":45422506,"url":"https://github.com/oszuidwest/bash-functions","last_synced_at":"2026-02-22T01:42:41.854Z","repository":{"id":204787608,"uuid":"658462128","full_name":"oszuidwest/bash-functions","owner":"oszuidwest","description":"A collection of utility functions to streamline our bash scripts","archived":false,"fork":false,"pushed_at":"2026-01-02T12:02:47.000Z","size":100,"stargazers_count":1,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2026-01-08T14:18:09.282Z","etag":null,"topics":["bash","shell"],"latest_commit_sha":null,"homepage":"","language":"Shell","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/oszuidwest.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2023-06-25T20:11:33.000Z","updated_at":"2025-12-23T23:32:18.000Z","dependencies_parsed_at":"2024-03-19T15:31:19.897Z","dependency_job_id":"d84fb498-cc1c-4c4e-9f40-c9cdb63cfd1b","html_url":"https://github.com/oszuidwest/bash-functions","commit_stats":null,"previous_names":["oszuidwest/bash-functions"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/oszuidwest/bash-functions","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/oszuidwest%2Fbash-functions","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/oszuidwest%2Fbash-functions/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/oszuidwest%2Fbash-functions/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/oszuidwest%2Fbash-functions/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/oszuidwest","download_url":"https://codeload.github.com/oszuidwest/bash-functions/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/oszuidwest%2Fbash-functions/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29703227,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-21T23:35:04.139Z","status":"ssl_error","status_checked_at":"2026-02-21T23:35:03.832Z","response_time":107,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["bash","shell"],"created_at":"2026-02-22T01:42:40.900Z","updated_at":"2026-02-22T01:42:41.846Z","avatar_url":"https://github.com/oszuidwest.png","language":"Shell","funding_links":[],"categories":[],"sub_categories":[],"readme":"# bash-functions\n\nThis is a Bash shell library that provides a set of common utility functions to ease and streamline the scripting process in Debian-like environments.\n\n## Function Reference\n\n### 1. `set_colors`\nInitializes color variables for terminal text formatting.\n\n**Parameters:** None\n\n**Example:**\n```bash\nset_colors\necho -e \"${GREEN}Success!${NC}\"\n```\n\n**Available colors:** GREEN, RED, YELLOW, BLUE, NC (No Color), BOLD, UNDERLINE\n\n---\n\n### 2. `backup_file`\nCreates a timestamped backup of a file if it exists.\n\n**Parameters:**\n- `$1` - The path to the file to backup\n\n**Example:**\n```bash\nbackup_file \"/etc/config.conf\"\n# Creates: /etc/config.conf.bak.20240206123045\n```\n\n---\n\n### 3. `is_silent`\nChecks if the first parameter is 'silent' to determine if output should be suppressed.\n\n**Parameters:**\n- `$1` - (Optional) Pass \"silent\" to return true\n\n**Returns:** 0 if silent, 1 otherwise\n\n**Example:**\n```bash\nif is_silent \"$1\"; then\n    echo \"Running in silent mode\"\nfi\n```\n\n---\n\n### 4. `is_this_linux`\nVerifies that the script is running on a Linux distribution.\n\n**Parameters:** None\n\n**Exits:** With code 1 if not running on Linux\n\n---\n\n### 5. `is_this_os_64bit`\nVerifies that the system is running a 64-bit operating system.\n\n**Parameters:** None\n\n**Exits:** With code 1 if not 64-bit\n\n---\n\n### 6. `check_rpi_model`\nChecks if running on a Raspberry Pi and verifies minimum model requirement.\n\n**Parameters:**\n- `$1` - The minimal Raspberry Pi model number required\n\n**Example:**\n```bash\ncheck_rpi_model 4  # Requires Raspberry Pi 4 or higher\n```\n\n---\n\n### 7. `check_user_privileges`\nVerifies that the script is running with the correct user privileges.\n\n**Parameters:**\n- `$1` - Either \"privileged\" (must be root) or \"regular\" (must not be root)\n\n**Example:**\n```bash\ncheck_user_privileges \"privileged\"  # Requires root\ncheck_user_privileges \"regular\"     # Requires non-root\n```\n\n---\n\n### 8. `check_apt`\nVerifies that the 'apt' package manager is available.\n\n**Parameters:** None\n\n**Exits:** With code 1 if apt is not installed\n\n---\n\n### 9. `update_os`\nUpdates the operating system using apt package manager.\n\n**Parameters:**\n- `$1` - (Optional) Pass \"silent\" to suppress output\n\n**Example:**\n```bash\nupdate_os          # Verbose mode\nupdate_os silent   # Silent mode\n```\n\n---\n\n### 10. `install_packages`\nInstalls one or more packages using apt package manager.\n\n**Parameters:**\n- `$1` - (Optional) Pass \"silent\" as first argument to suppress output\n- `$@` - Package names to install\n\n**Example:**\n```bash\ninstall_packages nginx mysql-server php\ninstall_packages silent nginx mysql-server php\n```\n\n---\n\n### 11. `set_timezone`\nSets the system timezone.\n\n**Parameters:**\n- `$1` - Valid timezone (e.g., \"Europe/Amsterdam\")\n\n**Example:**\n```bash\nset_timezone \"Europe/Amsterdam\"\nset_timezone \"America/New_York\"\n```\n\n---\n\n### 12. `require_tool`\nChecks for required tools and exits if any are missing.\n\n**Parameters:**\n- `$@` - Names of commands/tools to check for\n\n**Example:**\n```bash\nrequire_tool git curl wget\n```\n\n---\n\n### 13. `validate_input`\nValidates input based on a specified type.\n\n**Parameters:**\n- `$1` - The input value to validate\n- `$2` - The type of the variable: \"y/n\", \"num\", \"str\", \"email\", or \"host\"\n- `$3` - The name of the variable (used for error messages)\n\n**Returns:** 0 if the input is valid, 1 otherwise.\n\n**Example:**\n```bash\nif validate_input \"yes\" \"y/n\" \"CONFIRMATION\"; then\n    echo \"Valid input\"\nelse\n    echo \"Invalid input\"\nfi\n```\n\n---\n\n### 14. `ask_user`\nPrompts the user for input with validation and default values. The function follows two paths:\n\n1. **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.\n\n2. **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.\n\nThis ensures that the script can operate non-interactively when environment variables are pre-set, while still enforcing validation.\n\n**Parameters:**\n- `$1` - Variable name to store the result\n- `$2` - Default value if user presses Enter\n- `$3` - Prompt message to display\n- `$4` - (Optional) Validation type: \"y/n\", \"num\", \"str\", \"email\", or \"host\" (default: \"str\")\n\n**Example:**\n```bash\nask_user \"USERNAME\" \"admin\" \"Enter username\" \"str\"\nask_user \"PORT\" \"8080\" \"Enter port number\" \"num\"\nask_user \"ENABLE_SSL\" \"y\" \"Enable SSL?\" \"y/n\"\nask_user \"EMAIL\" \"admin@example.com\" \"Enter email\" \"email\"\nask_user \"HOSTNAME\" \"server.local\" \"Enter hostname\" \"host\"\n```\n\n---\n\n### 14. `download_file`\nDownloads files using curl with error handling and optional backup.\n\n**Single file mode:**\n```bash\ndownload_file URL DEST DESCRIPTION [backup]\n```\n\n**Multi-file mode:**\n```bash\ndownload_file -m DEST_DIR DESCRIPTION [backup] URL1:FILENAME1 URL2:FILENAME2\n```\n\n**Examples:**\n```bash\n# Single file\ndownload_file \"http://example.com/config.txt\" \"/etc/app/config.txt\" \"config file\"\n\n# Single file with backup\ndownload_file \"http://example.com/config.txt\" \"/etc/app/config.txt\" \"config file\" backup\n\n# Multiple files\ndownload_file -m \"/opt/app\" \"library files\" \\\n  \"http://example.com/lib1.so:lib1.so\" \\\n  \"http://example.com/lib2.so:lib2.so\"\n\n# Multiple files with backup\ndownload_file -m \"/opt/app\" \"library files\" backup \\\n  \"http://example.com/lib1.so:lib1.so\" \\\n  \"http://example.com/lib2.so:lib2.so\"\n```\n\n## How to Use\n\nThese functions can be imported and used in your own Bash scripts. To import the functions, use the following lines in your script:\n\n```bash\n# Remove old functions libraries and download the latest version\nrm -f /tmp/functions.sh\nif ! curl -s -o /tmp/functions.sh https://raw.githubusercontent.com/oszuidwest/bash-functions/main/common-functions.sh; then\n  echo -e \"*** Failed to download functions library. Please check your network connection! ***\"\n  exit 1\nfi\n\n# Source the functions file\nsource /tmp/functions.sh\n```\n\nAnd use it like this:\n\n```bash\n# Example\nset_colors\necho -e \"${GREEN}This text is green!${NC}\"\n```\n\nTo check for required tools:\n```bash\nrequire_tool git curl wget\n```\n\nOr this to ask the user for input, set a default (n) and validate the input (it should be y or n)\n\n```bash\nask_user \"SSL\" \"n\" \"Do you want Let's Encrypt to get a certificate for this server? (y/n)\" \"y/n\"\n```\n\nAll functions are documented inline with their parameters. \n\n### Note\nThis 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.\n\nAlways 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.\n\nBe 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.\n\n# License\nThis project is licensed under the MIT License - see the LICENSE.md file for details. \n\nBugs, feedback, and ideas are welcome at `techniek@zuidwesttv.nl` or through pull requests.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Foszuidwest%2Fbash-functions","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Foszuidwest%2Fbash-functions","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Foszuidwest%2Fbash-functions/lists"}