Added more functions to file.sh

This commit is contained in:
labbots
2020-06-07 16:11:58 +01:00
parent a21b8e1a03
commit b858f9d597
4 changed files with 112 additions and 5 deletions

View File

@@ -22,6 +22,8 @@ Bash library which provides utility functions and helpers for functional program
- [file::name()](#filename)
- [file::basename()](#filebasename)
- [file::extension()](#fileextension)
- [file::dirname()](#filedirname)
- [file::full_path()](#filefull_path)
- [Miscellaneous](#miscellaneous)
- [misc::check_internet_connection()](#misccheck_internet_connection)
- [String](#string)
@@ -223,7 +225,7 @@ echo "$(file::make_temp_file)"
tmp.vgftzy
```
_Function has no arguments._
*Function has no arguments.*
#### Exit codes
@@ -310,6 +312,57 @@ md
- extension of the file.
### file::dirname()
Get directory name from file path.
#### Example
```bash
echo "$(file::dirname "/path/to/test.md")"
#Output
/path/to
```
#### Arguments
- **$1** (string): path.
#### Exit codes
- **0**: If successful.
- **2**: Function missing arguments.
#### Output on stdout
- directory path.
### file::full_path()
Get absolute path of file or directory.
#### Example
```bash
file::full_path "../path/to/file.md"
#Output
/home/labbots/docs/path/to/file.md
```
#### Arguments
- **$1** (string): relative or absolute path to file/direcotry.
#### Exit codes
- **0**: If successful.
- **1**: If file/directory does not exist.
- **2**: Function missing arguments.
#### Output on stdout
- Absolute path to file/directory.
## Miscellaneous
Set of miscellaneous helper functions.
@@ -324,7 +377,7 @@ Check if internet connection is available.
misc::check_internet_connection
```
_Function has no arguments._
*Function has no arguments.*
#### Exit codes

View File

@@ -4,3 +4,4 @@ source src/array.sh
source src/string.sh
source src/variable.sh
source src/file.sh
source src/misc.sh

View File

@@ -28,7 +28,7 @@ BEGIN {
styles["li", "to"] = "- &"
styles["i", "from"] = ".*"
styles["i", "to"] = "_&_"
styles["i", "to"] = "*&*"
styles["anchor", "from"] = ".*"
styles["anchor", "to"] = "[&](#&)"

View File

@@ -66,7 +66,7 @@ file::basename() {
}
# @description Get the extension of file from file name.
#
# @example
# echo "$(file::extension "/path/to/test.md")"
# #Output
@@ -83,8 +83,61 @@ file::extension() {
[[ $# = 0 ]] && printf "%s: Missing arguments\n" "${FUNCNAME[0]}" && return 2
declare file extension
file="${1##*/}"
extension="${src_file##*.}"
extension="${file##*.}"
[[ "${file}" = "${extension}" ]] && return 1
printf "%s" "${extension}"
}
# @description Get directory name from file path.
#
# @example
# echo "$(file::dirname "/path/to/test.md")"
# #Output
# /path/to
#
# @arg $1 string path.
#
# @exitcode 0 If successful.
# @exitcode 2 Function missing arguments.
#
# @stdout directory path.
file::dirname() {
[[ $# = 0 ]] && printf "%s: Missing arguments\n" "${FUNCNAME[0]}" && return 2
declare tmp=${1:-.}
[[ ${tmp} != *[!/]* ]] && { printf '/\n' && return; }
tmp="${tmp%%"${tmp##*[!/]}"}"
[[ ${tmp} != */* ]] && { printf '.\n' && return; }
tmp=${tmp%/*} && tmp="${tmp%%"${tmp##*[!/]}"}"
printf '%s' "${tmp:-/}"
}
# @description Get absolute path of file or directory.
#
# @example
# file::full_path "../path/to/file.md"
# #Output
# /home/labbots/docs/path/to/file.md
#
# @arg $1 string relative or absolute path to file/direcotry.
#
# @exitcode 0 If successful.
# @exitcode 1 If file/directory does not exist.
# @exitcode 2 Function missing arguments.
#
# @stdout Absolute path to file/directory.
file::full_path() {
[[ $# = 0 ]] && printf "%s: Missing arguments\n" "${FUNCNAME[0]}" && return 2
declare input="${1}"
if [[ -f ${input} ]]; then
printf "%s/%s\n" "$(cd "$(file::dirname "${input}")" && pwd)" "${input##*/}" || return 1
elif [[ -d ${input} ]]; then
printf "%s\n" "$(cd "${input}" && pwd)"
else
return 1
fi
}