Skip to content
Snippets Groups Projects
Commit 68f9b3c8 authored by Brandon Rodriguez's avatar Brandon Rodriguez
Browse files

Create filename parse functions

parent 2668a43f
Branches
No related merge requests found
......@@ -21,8 +21,10 @@ text_cyan="\033[1;36m"
text_yellow="\033[1;33m"
text_white="\033[1;37m"
# Function Return.
# Function Return Variables.
return_value=""
file_name=""
file_extension=""
#endregion Global Utility Variables
......@@ -134,6 +136,72 @@ function get_directory_name () {
fi
}
###
# Parses passed file, getting base file name and file extension.
##
function parse_file_name () {
# Check number of passed function args.
if [[ ${#} == 1 ]]
then
# Expected number of args passed. Continue.
if [[ -f ${1} ]]
then
# Handle for file.
get_absolute_path ${1}
return_value=${return_value##*/}
file_name=""
file_extension=""
_recurse_file_extension ${return_value}
else
echo -e "${text_red}Passed value ( ${1} ) is not a valid file.${text_reset}"
exit 1
fi
# Handle for too many args.
elif [[ ${#} > 1 ]]
then
echo -e "${text_red}Too many args passed. Expected one arg, received ${#}.${text_reset}"
exit 1
# Handle for too few args.
else
echo -e "${text_red}Too few args passed. Expected one arg, received 0.${text_reset}"
exit 1
fi
}
###
# Recursive helper function for parse_file_name().
# Determines base file name and full file extension.
##
function _recurse_file_extension () {
local passed_value=${1}
local parsed_extension=${passed_value##*.}
# Check if file extension was found. Occurs when variables are not identical.
if [[ ${parsed_extension} != ${passed_value} ]]
then
# Extension found. Iterate once more.
file_name=${passed_value%.${parsed_extension}}
# Handle if global var is currently empty or not.
if [[ ${file_extension} == "" ]]
then
file_extension=".${parsed_extension}"
else
file_extension=".${parsed_extension}${file_extension}"
fi
# Call recursive function once more.
_recurse_file_extension ${file_name}
fi
}
#endregion Directory Functions
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment