From a21b8e1a033b78e2d080826512589274dd145f47 Mon Sep 17 00:00:00 2001 From: labbots Date: Sun, 7 Jun 2020 15:11:32 +0100 Subject: [PATCH] Added file functions --- README.md | 137 ++++++++++++++++++++++++++++++++++++++++++++++ bash_utilities.sh | 1 + src/file.sh | 90 ++++++++++++++++++++++++++++++ src/misc.sh | 36 ++++++++++++ 4 files changed, 264 insertions(+) create mode 100644 src/file.sh create mode 100644 src/misc.sh diff --git a/README.md b/README.md index 575547e..2a47190 100644 --- a/README.md +++ b/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) @@ -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" +## 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) diff --git a/bash_utilities.sh b/bash_utilities.sh index 4aa0c06..a0d5301 100644 --- a/bash_utilities.sh +++ b/bash_utilities.sh @@ -3,3 +3,4 @@ source src/array.sh source src/string.sh source src/variable.sh +source src/file.sh diff --git a/src/file.sh b/src/file.sh new file mode 100644 index 0000000..a4520e6 --- /dev/null +++ b/src/file.sh @@ -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}" +} diff --git a/src/misc.sh b/src/misc.sh new file mode 100644 index 0000000..58374cb --- /dev/null +++ b/src/misc.sh @@ -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 +}