mirror of
https://github.com/armbian/bash-util.git
synced 2026-01-06 10:37:49 -08:00
Added file functions
This commit is contained in:
137
README.md
137
README.md
@@ -17,6 +17,13 @@ Bash library which provides utility functions and helpers for functional program
|
||||
- [array::join()](#arrayjoin)
|
||||
- [array::reverse()](#arrayreverse)
|
||||
- [array::random_element()](#arrayrandom_element)
|
||||
- [File](#file)
|
||||
- [file::make_temp_file()](#filemake_temp_file)
|
||||
- [file::name()](#filename)
|
||||
- [file::basename()](#filebasename)
|
||||
- [file::extension()](#fileextension)
|
||||
- [Miscellaneous](#miscellaneous)
|
||||
- [misc::check_internet_connection()](#misccheck_internet_connection)
|
||||
- [String](#string)
|
||||
- [string::trim()](#stringtrim)
|
||||
- [string::split()](#stringsplit)
|
||||
@@ -36,6 +43,7 @@ Bash library which provides utility functions and helpers for functional program
|
||||
- [variable::is_bool()](#variableis_bool)
|
||||
- [variable::is_true()](#variableis_true)
|
||||
- [variable::is_false()](#variableis_false)
|
||||
- [Inspired By](#inspired-by)
|
||||
- [License](#license)
|
||||
|
||||
<!-- END generate_readme.sh generated TOC please keep comment here to allow auto update -->
|
||||
@@ -198,6 +206,131 @@ c
|
||||
|
||||
- Random item out of the array.
|
||||
|
||||
## File
|
||||
|
||||
Functions for handling files.
|
||||
|
||||
### file::make_temp_file()
|
||||
|
||||
Create temporary file.
|
||||
Function creates temporary file with random name. The temporary file will be deleted when script finishes.
|
||||
|
||||
#### Example
|
||||
|
||||
```bash
|
||||
echo "$(file::make_temp_file)"
|
||||
#Output
|
||||
tmp.vgftzy
|
||||
```
|
||||
|
||||
_Function has no arguments._
|
||||
|
||||
#### Exit codes
|
||||
|
||||
- **0**: If successful.
|
||||
- **1**: If failed to create temp file.
|
||||
|
||||
#### Output on stdout
|
||||
|
||||
- file name of temporary file created.
|
||||
|
||||
### file::name()
|
||||
|
||||
Get only the filename from string path.
|
||||
|
||||
#### Example
|
||||
|
||||
```bash
|
||||
echo "$(file::name "/path/to/test.md")"
|
||||
#Output
|
||||
test.md
|
||||
```
|
||||
|
||||
#### Arguments
|
||||
|
||||
- **$1** (string): path.
|
||||
|
||||
#### Exit codes
|
||||
|
||||
- **0**: If successful.
|
||||
- **2**: Function missing arguments.
|
||||
|
||||
#### Output on stdout
|
||||
|
||||
- name of the file with extension.
|
||||
|
||||
### file::basename()
|
||||
|
||||
Get the basename of file from file name.
|
||||
|
||||
#### Example
|
||||
|
||||
```bash
|
||||
echo "$(file::basename "/path/to/test.md")"
|
||||
#Output
|
||||
test
|
||||
```
|
||||
|
||||
#### Arguments
|
||||
|
||||
- **$1** (string): path.
|
||||
|
||||
#### Exit codes
|
||||
|
||||
- **0**: If successful.
|
||||
- **2**: Function missing arguments.
|
||||
|
||||
#### Output on stdout
|
||||
|
||||
- basename of the file.
|
||||
|
||||
### file::extension()
|
||||
|
||||
Get the extension of file from file name.
|
||||
|
||||
#### Example
|
||||
|
||||
```bash
|
||||
echo "$(file::extension "/path/to/test.md")"
|
||||
#Output
|
||||
md
|
||||
```
|
||||
|
||||
#### Arguments
|
||||
|
||||
- **$1** (string): path.
|
||||
|
||||
#### Exit codes
|
||||
|
||||
- **0**: If successful.
|
||||
- **1**: If no extension is found in the filename.
|
||||
- **2**: Function missing arguments.
|
||||
|
||||
#### Output on stdout
|
||||
|
||||
- extension of the file.
|
||||
|
||||
## Miscellaneous
|
||||
|
||||
Set of miscellaneous helper functions.
|
||||
|
||||
### misc::check_internet_connection()
|
||||
|
||||
Check if internet connection is available.
|
||||
|
||||
#### Example
|
||||
|
||||
```bash
|
||||
misc::check_internet_connection
|
||||
```
|
||||
|
||||
_Function has no arguments._
|
||||
|
||||
#### Exit codes
|
||||
|
||||
- **0**: If script can connect to internet.
|
||||
- **1**: If script cannot access internet.
|
||||
|
||||
## String
|
||||
|
||||
Functions for string operations and manipulations.
|
||||
@@ -594,6 +727,10 @@ variable::is_false "false"
|
||||
|
||||
<!-- END generate_readme.sh generated SHDOC please keep comment here to allow auto update -->
|
||||
|
||||
## Inspired By
|
||||
|
||||
- [Bash Bible](https://github.com/dylanaraps/pure-bash-bible) - A collection of pure bash alternatives to external processes.
|
||||
|
||||
## License
|
||||
|
||||
[MIT](https://github.com/labbots/google-drive-upload/blob/master/LICENSE)
|
||||
|
||||
@@ -3,3 +3,4 @@
|
||||
source src/array.sh
|
||||
source src/string.sh
|
||||
source src/variable.sh
|
||||
source src/file.sh
|
||||
|
||||
90
src/file.sh
Normal file
90
src/file.sh
Normal file
@@ -0,0 +1,90 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
# @file File
|
||||
# @brief Functions for handling files.
|
||||
|
||||
# @description Create temporary file.
|
||||
# Function creates temporary file with random name. The temporary file will be deleted when script finishes.
|
||||
#
|
||||
# @example
|
||||
# echo "$(file::make_temp_file)"
|
||||
# #Output
|
||||
# tmp.vgftzy
|
||||
#
|
||||
# @noargs
|
||||
#
|
||||
# @exitcode 0 If successful.
|
||||
# @exitcode 1 If failed to create temp file.
|
||||
#
|
||||
# @stdout file name of temporary file created.
|
||||
file::make_temp_file() {
|
||||
declare temp_file
|
||||
type -p mktemp &> /dev/null && { temp_file="$(mktemp -u)" || temp_file="${PWD}/$((RANDOM * 2)).LOG"; }
|
||||
trap 'rm -f "${temp_file}"' EXIT
|
||||
printf "%s" "${temp_file}"
|
||||
}
|
||||
|
||||
# @description Get only the filename from string path.
|
||||
|
||||
# @example
|
||||
# echo "$(file::name "/path/to/test.md")"
|
||||
# #Output
|
||||
# test.md
|
||||
#
|
||||
# @arg $1 string path.
|
||||
#
|
||||
# @exitcode 0 If successful.
|
||||
# @exitcode 2 Function missing arguments.
|
||||
#
|
||||
# @stdout name of the file with extension.
|
||||
file::name() {
|
||||
[[ $# = 0 ]] && printf "%s: Missing arguments\n" "${FUNCNAME[0]}" && return 2
|
||||
printf "%s" "${1##*/}"
|
||||
}
|
||||
|
||||
# @description Get the basename of file from file name.
|
||||
|
||||
# @example
|
||||
# echo "$(file::basename "/path/to/test.md")"
|
||||
# #Output
|
||||
# test
|
||||
#
|
||||
# @arg $1 string path.
|
||||
#
|
||||
# @exitcode 0 If successful.
|
||||
# @exitcode 2 Function missing arguments.
|
||||
#
|
||||
# @stdout basename of the file.
|
||||
file::basename() {
|
||||
[[ $# = 0 ]] && printf "%s: Missing arguments\n" "${FUNCNAME[0]}" && return 2
|
||||
|
||||
declare file basename
|
||||
file="${1##*/}"
|
||||
basename="${file%.*}"
|
||||
|
||||
printf "%s" "${basename}"
|
||||
}
|
||||
|
||||
# @description Get the extension of file from file name.
|
||||
|
||||
# @example
|
||||
# echo "$(file::extension "/path/to/test.md")"
|
||||
# #Output
|
||||
# md
|
||||
#
|
||||
# @arg $1 string path.
|
||||
#
|
||||
# @exitcode 0 If successful.
|
||||
# @exitcode 1 If no extension is found in the filename.
|
||||
# @exitcode 2 Function missing arguments.
|
||||
#
|
||||
# @stdout extension of the file.
|
||||
file::extension() {
|
||||
[[ $# = 0 ]] && printf "%s: Missing arguments\n" "${FUNCNAME[0]}" && return 2
|
||||
declare file extension
|
||||
file="${1##*/}"
|
||||
extension="${src_file##*.}"
|
||||
[[ "${file}" = "${extension}" ]] && return 1
|
||||
|
||||
printf "%s" "${extension}"
|
||||
}
|
||||
36
src/misc.sh
Normal file
36
src/misc.sh
Normal file
@@ -0,0 +1,36 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
# @file Miscellaneous
|
||||
# @brief Set of miscellaneous helper functions.
|
||||
|
||||
# @internal
|
||||
# @description Check if script is run in terminal.
|
||||
#
|
||||
# @noargs
|
||||
#
|
||||
# @exitcode 0 If script is run on terminal.
|
||||
# @exitcode 1 If script is not run on terminal.
|
||||
_is_terminal() {
|
||||
[[ -t 1 || -z ${TERM} ]] && return 0 || return 1
|
||||
}
|
||||
|
||||
# @description Check if internet connection is available.
|
||||
#
|
||||
# @example
|
||||
# misc::check_internet_connection
|
||||
#
|
||||
# @noargs
|
||||
#
|
||||
# @exitcode 0 If script can connect to internet.
|
||||
# @exitcode 1 If script cannot access internet.
|
||||
misc::check_internet_connection() {
|
||||
declare check_internet
|
||||
if _is_terminal; then
|
||||
check_internet="$(sh -ic 'exec 3>&1 2>/dev/null; { curl --compressed -Is google.com 1>&3; kill 0; } | { sleep 10; kill 0; }' || :)"
|
||||
else
|
||||
check_internet="$(curl --compressed -Is google.com -m 10)"
|
||||
fi
|
||||
if [[ -z ${check_internet} ]]; then
|
||||
return 1
|
||||
fi
|
||||
}
|
||||
Reference in New Issue
Block a user