Script to add extension to a list of files

This commit is contained in:
gardouille 2021-01-06 15:57:05 +01:00
parent 02e7b71959
commit 82a03d8864
Signed by: gardouille
GPG Key ID: E759BAA22501AF32

289
ext.add.sh Executable file
View File

@ -0,0 +1,289 @@
#!/bin/sh
# Purpose: Add the first argument as extension to other arguments
# Vars {{{
readonly PROGNAME=$(basename "${0}")
readonly NBARGS="${#}"
## Test if DEBUG is already defined (by parent script,…)
[ -z "${DEBUG}" ] && DEBUG=1
## Colors
readonly PURPLE='\033[1;35m'
readonly RED='\033[0;31m'
readonly RESET='\033[0m'
readonly COLOR_DEBUG="${PURPLE}"
# }}}
usage() { # {{{
cat <<- EOF
usage: $PROGNAME [--debug,--help,--extension]
Try to add a file's extension.
EXAMPLES:
- Add "bkp" extension to /tmp/test.me file
${PROGNAME} bkp /tmp/test.me
OPTIONS:
-d,--debug
Enable debug messages.
-e,--extension
Define the extension to use.
-h,--help
Print this help message.
EOF
}
# }}}
debug_message() { # {{{
local_debug_message="${1}"
## Print message if DEBUG is enable (=0)
[ "${DEBUG}" -eq "0" ] && printf '\e[1;35m%-6b\e[m\n' "DEBUG ${PROGNAME}: ${local_debug_message}"
local_debug_message=""
return 0
}
# }}}
error_message() { # {{{
local_error_message="${1}"
local_error_code="${2}"
## Print message if DEBUG is enable (=0)
[ "${DEBUG}" -eq "0" ] && printf '%b\n' "ERROR ${PROGNAME}: ${RED}${local_error_message}${RESET}"
local_error_message=""
exit "${local_error_code:=66}"
}
# }}}
is_file() { # {{{
local_file="${1}"
if [ -e "${local_file}" ]; then
return_is_file="0"
debug_message "is_file \
${local_file} seems to exists."
else
return_is_file="1"
debug_message "is_file \
${local_file} doesn't seems to exists."
fi
## Empty var
local_file=""
return "${return_is_file}"
}
# }}}
is_not_file() { # {{{
local_file="${1}"
if [ ! -e "${local_file}" ]; then
return_is_not_file="0"
debug_message "is_not_file \
${local_file} doesn't seems to exists."
else
return_is_not_file="1"
debug_message "is_not_file \
${local_file} seems to exists."
fi
## Empty var
local_file=""
return "${return_is_not_file}"
}
# }}}
is_defined() { # {{{
local_var="${1}"
local_var_name="${2}"
if [ -n "${local_var}" ]; then
return_is_defined="0"
debug_message "is_defined \
${local_var_name:=VAR} (${local_var}) seems defined."
else
return_is_defined="1"
debug_message "is_defined \
${local_var_name:=VAR} doesn't seems defined."
fi
## Empty var
local_var=""
local_var_name=""
return "${return_is_defined}"
}
# }}}
is_not_defined() { # {{{
local_var="${1}"
local_var_name="${2}"
if [ -z "${local_var}" ]; then
return_is_not_defined="0"
debug_message "is_not_defined \
${local_var_name:=VAR} doesn't seems defined."
else
return_is_not_defined="1"
debug_message "is_not_defined \
${local_var_name:=VAR} (${local_var}) seems defined."
fi
## Empty var
local_var=""
local_var_name=""
return "${return_is_not_defined}"
}
# }}}
add_extension() { # {{{
local_extension="${1}"
local_file="${2}"
file_with_extension="${local_file}.${local_extension}"
debug_message "add_extension \
Try to add '${local_extension} to ${local_file} file."
mv -- "${local_file}" "${file_with_extension}"
return_add_extension="${?}"
## Empty var
local_extension=""
local_file=""
return "${return_add_extension}"
}
# }}}
main() { # {{{
is_not_defined "${FILE_LIST}" FILE_LIST && \
usage && \
error_message "Some file or directory must be passed as argument" "33"
IFS="%"
for file in ${FILE_LIST}; do
debug_message "FILE_LIST for loop \
Try to manage ${file} file"
add_extension "${EXTENSION}" "${file}"
done
}
# }}}
# Manage arguments # {{{
## If there is argument(s)
if [ ! "${NBARGS}" -eq "0" ]; then
manage_arg="0"
# Parse all options (start with a "-") one by one {{{
while printf -- '%s' "${1}" | grep -q -E -- "^-+"; do
case "${1}" in
-d|--debug ) ## Enable debug mode
## Enable DEBUG
DEBUG="0"
;;
-e|--extension ) ## Define the extension
## Move to the next argument
shift
## Define the extension with this argument
EXTENSION="${1}"
;;
-h|--help ) ## help
usage
## Exit after help informations
exit 0
;;
* ) ## unknow option
printf '%b\n' "${RED}Invalid option: ${1}${RESET}"
printf '%b\n' "---"
usage
exit 1
;;
esac
debug_message "Arguments management \
${RED}${1}${COLOR_DEBUG} option managed."
## Move to the next argument
shift
manage_arg=$((manage_arg+1))
done
# }}}
# If it remains arguments (not option) {{{
if [ "${1}" ]; then
debug_message "Arguments management \
Remains arguments."
while printf -- '%s' "${1}" | grep -q -E -- "^.+"; do
temp_arg="${1}"
debug_message "Arguments management \
Manage ${temp_arg}"
### If arg is a file|directory|…
is_file "${temp_arg}" && {
### Add arg to FILE_LIST if it's already defined
is_defined "${FILE_LIST}" FILE_LIST && \
FILE_LIST="${FILE_LIST}%${temp_arg}" || \
FILE_LIST="${temp_arg}"
}
### If arg is not a file|directory|…
is_not_file "${temp_arg}" && {
### If EXTENSION was already defined, skip
is_defined "${EXTENSION}" EXTENSION && \
printf '%s\n' "${temp_arg} is not a file and a EXTENSION was already defined. Skipping."
### If not defined, use this arg as EXTENSION
is_not_defined "${EXTENSION}" EXTENSION && \
EXTENSION="${temp_arg}"
}
### Move to the next argument
shift
manage_arg=$((manage_arg+1))
done
else
debug_message "Arguments management \
No more arguments."
fi
# }}}
debug_message "Arguments management \
${RED}${manage_arg}${COLOR_DEBUG} argument(s) successfully managed."
else
debug_message "Arguments management \
No arguments/options to manage."
fi
# }}}
main
exit 0