Files
bash-util/README.md

600 lines
10 KiB
Markdown
Raw Normal View History

2020-06-06 21:07:20 +01:00
<h1 align="center">Bash Utilites</h1>
<p align="center">
<a href="https://github.com/labbots/bash-utility/blob/master/LICENSE"><img src="https://img.shields.io/github/license/labbots/bash-utility.svg?style=for-the-badge" alt="License"></a>
<a href="https://www.codacy.com/manual/labbots/bash-utility?utm_source=github.com&amp;utm_medium=referral&amp;utm_content=labbots/bash-utility&amp;utm_campaign=Badge_Grade"><img src="https://img.shields.io/codacy/grade/99fbe8d389254b6ebb37899ce89658e3?style=for-the-badge"/></a>
</p>
2020-06-05 11:15:59 +01:00
Bash library which provides utility functions and helpers for functional programming in Bash.
2020-06-06 21:07:20 +01:00
<!-- START generate_readme.sh generated TOC please keep comment here to allow auto update -->
<!-- DO NOT EDIT THIS SECTION, INSTEAD RE-RUN generate_readme.sh TO UPDATE -->
2020-06-06 21:24:37 +01:00
## Table of Contents
2020-06-06 21:07:20 +01:00
- [Array](#array)
- [array::contains()](#arraycontains)
- [array::dedupe()](#arraydedupe)
- [array::is_empty()](#arrayis_empty)
2020-06-06 21:07:20 +01:00
- [array::join()](#arrayjoin)
- [array::reverse()](#arrayreverse)
- [array::random_element()](#arrayrandom_element)
- [String](#string)
- [string::trim()](#stringtrim)
- [string::split()](#stringsplit)
- [string::lstrip()](#stringlstrip)
- [string::rstrip()](#stringrstrip)
- [string::to_lower()](#stringto_lower)
- [string::to_upper()](#stringto_upper)
- [string::contains()](#stringcontains)
- [string::starts_with()](#stringstarts_with)
- [string::ends_with()](#stringends_with)
- [string::regex()](#stringregex)
2020-06-07 14:13:09 +01:00
- [Variable](#variable)
- [variable::is_array()](#variableis_array)
- [variable::is_numeric()](#variableis_numeric)
- [variable::is_int()](#variableis_int)
- [variable::is_float()](#variableis_float)
- [variable::is_bool()](#variableis_bool)
- [variable::is_true()](#variableis_true)
- [variable::is_false()](#variableis_false)
2020-06-06 21:24:37 +01:00
- [License](#license)
2020-06-06 21:07:20 +01:00
<!-- END generate_readme.sh generated TOC please keep comment here to allow auto update -->
<!-- START generate_readme.sh generated SHDOC please keep comment here to allow auto update -->
<!-- DO NOT EDIT THIS SECTION, INSTEAD RE-RUN generate_readme.sh TO UPDATE -->
2020-06-06 21:24:37 +01:00
## Array
2020-06-06 21:07:20 +01:00
Functions for array operations and manipulations.
2020-06-06 21:24:37 +01:00
### array::contains()
2020-06-06 21:07:20 +01:00
Check if item exists in the given array.
2020-06-06 21:24:37 +01:00
#### Example
2020-06-06 21:07:20 +01:00
```bash
array=("a" "b" "c")
array::contains "c" ${array[@]}
#Output
0
```
2020-06-06 21:24:37 +01:00
#### Arguments
2020-06-06 21:07:20 +01:00
- **$1** (mixed): Item to search (needle).
- **$2** (array): array to be searched (haystack).
2020-06-06 21:24:37 +01:00
#### Exit codes
2020-06-06 21:07:20 +01:00
- **0**: If successful.
- **1**: If no match found in the array.
- **2**: Function missing arguments.
2020-06-06 21:24:37 +01:00
### array::dedupe()
2020-06-06 21:07:20 +01:00
Remove duplicate items from the array.
2020-06-06 21:07:20 +01:00
2020-06-06 21:24:37 +01:00
#### Example
2020-06-06 21:07:20 +01:00
```bash
array=("a" "b" "a" "c")
printf "%s" "$(array::dedupe ${array[@]})"
#Output
a b c
```
2020-06-06 21:24:37 +01:00
#### Arguments
2020-06-06 21:07:20 +01:00
- **$1** (array): Array to be deduped.
2020-06-06 21:24:37 +01:00
#### Exit codes
2020-06-06 21:07:20 +01:00
- **0**: If successful.
- **2**: Function missing arguments.
2020-06-06 21:24:37 +01:00
#### Output on stdout
2020-06-06 21:07:20 +01:00
- Deduplicated array.
### array::is_empty()
Check if a given array is empty.
#### Example
```bash
array=("a" "b" "c" "d")
array::is_empty "${array[@]}"
```
#### Arguments
- **$1** (array): Array to be checked.
#### Exit codes
- **0**: If the given array is empty.
- **2**: If the given array is not empty.
2020-06-06 21:24:37 +01:00
### array::join()
2020-06-06 21:07:20 +01:00
Join array elements with a string.
2020-06-06 21:24:37 +01:00
#### Example
2020-06-06 21:07:20 +01:00
```bash
array=("a" "b" "c" "d")
printf "%s" "$(array::join "," "${array[@]}")"
#Output
a,b,c,d
printf "%s" "$(array::join "" "${array[@]}")"
#Output
abcd
```
2020-06-06 21:24:37 +01:00
#### Arguments
2020-06-06 21:07:20 +01:00
- **$1** (string): String to join the array elements (glue).
- **$2** (array): array to be joined with glue string.
2020-06-06 21:24:37 +01:00
#### Exit codes
2020-06-06 21:07:20 +01:00
- **0**: If successful.
- **2**: Function missing arguments.
2020-06-06 21:24:37 +01:00
#### Output on stdout
2020-06-06 21:07:20 +01:00
- String containing a string representation of all the array elements in the same order,with the glue string between each element.
2020-06-06 21:24:37 +01:00
### array::reverse()
2020-06-06 21:07:20 +01:00
Return an array with elements in reverse order.
2020-06-06 21:24:37 +01:00
#### Example
2020-06-06 21:07:20 +01:00
```bash
array=(1 2 3 4 5)
printf "%s" "$(array::reverse "${array[@]}")"
#Output
5 4 3 2 1
```
2020-06-06 21:24:37 +01:00
#### Arguments
2020-06-06 21:07:20 +01:00
- **$1** (array): The input array.
2020-06-06 21:24:37 +01:00
#### Exit codes
2020-06-06 21:07:20 +01:00
- **0**: If successful.
- **2**: Function missing arguments.
2020-06-06 21:24:37 +01:00
#### Output on stdout
2020-06-06 21:07:20 +01:00
- The reversed array.
2020-06-06 21:24:37 +01:00
### array::random_element()
2020-06-06 21:07:20 +01:00
Returns a random item from the array.
2020-06-06 21:24:37 +01:00
#### Example
2020-06-06 21:07:20 +01:00
```bash
array=("a" "b" "c" "d")
printf "%s\n" "$(array::random_element "${array[@]}")"
#Output
c
```
2020-06-06 21:24:37 +01:00
#### Arguments
2020-06-06 21:07:20 +01:00
- **$1** (array): The input array.
2020-06-06 21:24:37 +01:00
#### Exit codes
2020-06-06 21:07:20 +01:00
- **0**: If successful.
- **2**: Function missing arguments.
2020-06-06 21:24:37 +01:00
#### Output on stdout
2020-06-06 21:07:20 +01:00
- Random item out of the array.
2020-06-06 21:24:37 +01:00
## String
2020-06-06 21:07:20 +01:00
Functions for string operations and manipulations.
2020-06-06 21:24:37 +01:00
### string::trim()
2020-06-06 21:07:20 +01:00
Strip whitespace from the beginning and end of a string.
2020-06-06 21:24:37 +01:00
#### Example
2020-06-06 21:07:20 +01:00
```bash
echo "$(string::trim " Hello World! ")"
#Output
Hello World!
```
2020-06-06 21:24:37 +01:00
#### Arguments
2020-06-06 21:07:20 +01:00
- **$1** (The): string that will be trimmed.
2020-06-06 21:24:37 +01:00
#### Exit codes
2020-06-06 21:07:20 +01:00
- **0**: If successful.
- **2**: Function missing arguments.
2020-06-06 21:24:37 +01:00
#### Output on stdout
2020-06-06 21:07:20 +01:00
- The trimmed string.
2020-06-06 21:24:37 +01:00
### string::split()
2020-06-06 21:07:20 +01:00
Split a string to array by a delimiter.
2020-06-06 21:24:37 +01:00
#### Example
2020-06-06 21:07:20 +01:00
```bash
printf "%s" "$(string::split "Hello!World" "!")"
#Output
Hello
World
```
2020-06-06 21:24:37 +01:00
#### Arguments
2020-06-06 21:07:20 +01:00
- **$1** (string): The input string.
- **$2** (string): The delimiter string.
2020-06-06 21:24:37 +01:00
#### Exit codes
2020-06-06 21:07:20 +01:00
- **0**: If successful.
- **2**: Function missing arguments.
2020-06-06 21:24:37 +01:00
#### Output on stdout
2020-06-06 21:07:20 +01:00
- Returns an array of strings created by splitting the string parameter by the delimiter.
2020-06-06 21:24:37 +01:00
### string::lstrip()
2020-06-06 21:07:20 +01:00
Strip characters from the beginning of a string.
2020-06-06 21:24:37 +01:00
#### Example
2020-06-06 21:07:20 +01:00
```bash
echo "$(string::lstrip "Hello World!" "He")"
#Output
llo World!
```
2020-06-06 21:24:37 +01:00
#### Arguments
2020-06-06 21:07:20 +01:00
- **$1** (string): The input string.
- **$2** (string): The characters you want to strip.
2020-06-06 21:24:37 +01:00
#### Exit codes
2020-06-06 21:07:20 +01:00
- **0**: If successful.
- **2**: Function missing arguments.
2020-06-06 21:24:37 +01:00
#### Output on stdout
2020-06-06 21:07:20 +01:00
- Returns the modified string.
2020-06-06 21:24:37 +01:00
### string::rstrip()
2020-06-06 21:07:20 +01:00
Strip characters from the end of a string.
2020-06-06 21:24:37 +01:00
#### Example
2020-06-06 21:07:20 +01:00
```bash
echo "$(string::rstrip "Hello World!" "d!")"
#Output
Hello Worl
```
2020-06-06 21:24:37 +01:00
#### Arguments
2020-06-06 21:07:20 +01:00
- **$1** (string): The input string.
- **$2** (string): The characters you want to strip.
2020-06-06 21:24:37 +01:00
#### Exit codes
2020-06-06 21:07:20 +01:00
- **0**: If successful.
- **2**: Function missing arguments.
2020-06-06 21:24:37 +01:00
#### Output on stdout
2020-06-06 21:07:20 +01:00
- Returns the modified string.
2020-06-06 21:24:37 +01:00
### string::to_lower()
2020-06-06 21:07:20 +01:00
Make a string lowercase.
2020-06-06 21:24:37 +01:00
#### Example
2020-06-06 21:07:20 +01:00
```bash
echo "$(string::to_lower "HellO")"
#Output
hello
```
2020-06-06 21:24:37 +01:00
#### Arguments
2020-06-06 21:07:20 +01:00
- **$1** (string): The input string.
2020-06-06 21:24:37 +01:00
#### Exit codes
2020-06-06 21:07:20 +01:00
- **0**: If successful.
- **2**: Function missing arguments.
2020-06-06 21:24:37 +01:00
#### Output on stdout
2020-06-06 21:07:20 +01:00
- Returns the lowercased string.
2020-06-06 21:24:37 +01:00
### string::to_upper()
2020-06-06 21:07:20 +01:00
Make a string all uppercase.
2020-06-06 21:24:37 +01:00
#### Example
2020-06-06 21:07:20 +01:00
```bash
echo "$(string::to_upper "HellO")"
#Output
HELLO
```
2020-06-06 21:24:37 +01:00
#### Arguments
2020-06-06 21:07:20 +01:00
- **$1** (string): The input string.
2020-06-06 21:24:37 +01:00
#### Exit codes
2020-06-06 21:07:20 +01:00
- **0**: If successful.
- **2**: Function missing arguments.
2020-06-06 21:24:37 +01:00
#### Output on stdout
2020-06-06 21:07:20 +01:00
- Returns the uppercased string.
2020-06-06 21:24:37 +01:00
### string::contains()
2020-06-06 21:07:20 +01:00
Check whether the search string exists within the input string.
2020-06-06 21:24:37 +01:00
#### Example
2020-06-06 21:07:20 +01:00
```bash
string::contains "Hello World!" "lo"
```
2020-06-06 21:24:37 +01:00
#### Arguments
2020-06-06 21:07:20 +01:00
- **$1** (string): The input string.
- **$2** (string): The search key.
2020-06-06 21:24:37 +01:00
#### Exit codes
2020-06-06 21:07:20 +01:00
- **0**: If match found.
- **1**: If no match found.
- **2**: Function missing arguments.
2020-06-06 21:24:37 +01:00
### string::starts_with()
2020-06-06 21:07:20 +01:00
Check whether the input string starts with key string.
2020-06-06 21:24:37 +01:00
#### Example
2020-06-06 21:07:20 +01:00
```bash
string::starts_with "Hello World!" "He"
```
2020-06-06 21:24:37 +01:00
#### Arguments
2020-06-06 21:07:20 +01:00
- **$1** (string): The input string.
- **$2** (string): The search key.
2020-06-06 21:24:37 +01:00
#### Exit codes
2020-06-06 21:07:20 +01:00
- **0**: If match found.
- **1**: If no match found.
- **2**: Function missing arguments.
2020-06-06 21:24:37 +01:00
### string::ends_with()
2020-06-06 21:07:20 +01:00
Check whether the input string ends with key string.
2020-06-06 21:24:37 +01:00
#### Example
2020-06-06 21:07:20 +01:00
```bash
string::ends_with "Hello World!" "d!"
```
2020-06-06 21:24:37 +01:00
#### Arguments
2020-06-06 21:07:20 +01:00
- **$1** (string): The input string.
- **$2** (string): The search key.
2020-06-06 21:24:37 +01:00
#### Exit codes
2020-06-06 21:07:20 +01:00
- **0**: If match found.
- **1**: If no match found.
- **2**: Function missing arguments.
2020-06-06 21:24:37 +01:00
### string::regex()
2020-06-06 21:07:20 +01:00
Check whether the input string matches the given regex.
2020-06-06 21:24:37 +01:00
#### Example
2020-06-06 21:07:20 +01:00
```bash
string::regex "HELLO" "^[A-Z]*$"
```
2020-06-06 21:24:37 +01:00
#### Arguments
2020-06-06 21:07:20 +01:00
- **$1** (string): The input string.
- **$2** (string): The search key.
2020-06-06 21:24:37 +01:00
#### Exit codes
2020-06-06 21:07:20 +01:00
- **0**: If match found.
- **1**: If no match found.
- **2**: Function missing arguments.
2020-06-07 14:13:09 +01:00
## Variable
Functions for handling variables.
### variable::is_array()
Check if given variable is array.
#### Example
```bash
array=("a" "b" "c")
variable::is_array "${array[@]}"
#Output
0
```
#### Arguments
- **$1** (mixed): Value of variable to check.
#### Exit codes
- **0**: If input is array.
- **1**: If input is not an array.
### variable::is_numeric()
Check if given variable is a number.
#### Example
```bash
variable::is_numeric "1234"
#Output
0
```
#### Arguments
- **$1** (mixed): Value of variable to check.
#### Exit codes
- **0**: If input is number.
- **1**: If input is not a number.
### variable::is_int()
Check if given variable is an integer.
#### Example
```bash
variable::is_int "+1234"
#Output
0
```
#### Arguments
- **$1** (mixed): Value of variable to check.
#### Exit codes
- **0**: If input is an integer.
- **1**: If input is not an integer.
### variable::is_float()
Check if given variable is a float.
#### Example
```bash
variable::is_float "+1234.0"
#Output
0
```
#### Arguments
- **$1** (mixed): Value of variable to check.
#### Exit codes
- **0**: If input is a float.
- **1**: If input is not a float.
### variable::is_bool()
Check if given variable is a boolean.
#### Example
```bash
variable::is_bool "true"
#Output
0
```
#### Arguments
- **$1** (mixed): Value of variable to check.
#### Exit codes
- **0**: If input is a boolean.
- **1**: If input is not a boolean.
### variable::is_true()
Check if given variable is a true.
#### Example
```bash
variable::is_true "true"
#Output
0
```
#### Arguments
- **$1** (mixed): Value of variable to check.
#### Exit codes
- **0**: If input is true.
- **1**: If input is not true.
### variable::is_false()
Check if given variable is false.
#### Example
```bash
variable::is_false "false"
#Output
0
```
#### Arguments
- **$1** (mixed): Value of variable to check.
#### Exit codes
- **0**: If input is false.
- **1**: If input is not false.
2020-06-06 21:07:20 +01:00
<!-- END generate_readme.sh generated SHDOC please keep comment here to allow auto update -->
## License
[MIT](https://github.com/labbots/google-drive-upload/blob/master/LICENSE)