mirror of
https://github.com/natelandau/shell-scripting-templates.git
synced 2025-11-11 06:23:47 -05:00
Updates to formatting
This commit is contained in:
59
bin/trash
59
bin/trash
@@ -56,12 +56,12 @@ function trapCleanup() {
|
|||||||
# Flags which can be overridden by user input.
|
# Flags which can be overridden by user input.
|
||||||
# Default values are below
|
# Default values are below
|
||||||
# -----------------------------------
|
# -----------------------------------
|
||||||
quiet=0
|
quiet=false
|
||||||
printLog=0
|
printLog=false
|
||||||
verbose=0
|
verbose=false
|
||||||
force=0
|
force=false
|
||||||
strict=0
|
strict=false
|
||||||
debug=0
|
debug=false
|
||||||
list=false
|
list=false
|
||||||
emptyTrash=false
|
emptyTrash=false
|
||||||
secureEmpty=false
|
secureEmpty=false
|
||||||
@@ -146,13 +146,13 @@ function realpath() {
|
|||||||
fi
|
fi
|
||||||
|
|
||||||
# attempt to change to the directory
|
# attempt to change to the directory
|
||||||
if ! cd "$directory" &>/dev/null ; then
|
if ! cd "${directory}" &>/dev/null ; then
|
||||||
success=false
|
success=false
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if $success; then
|
if ${success}; then
|
||||||
# does the filename exist?
|
# does the filename exist?
|
||||||
if [[ ( -n "$file_basename" ) && ( ! -e "$file_basename" ) ]]; then
|
if [[ ( -n "${file_basename}" ) && ( ! -e "${file_basename}" ) ]]; then
|
||||||
success=false
|
success=false
|
||||||
fi
|
fi
|
||||||
|
|
||||||
@@ -168,11 +168,11 @@ function realpath() {
|
|||||||
fi
|
fi
|
||||||
|
|
||||||
# output the absolute path
|
# output the absolute path
|
||||||
echo "$abs_path"
|
echo "${abs_path}"
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
|
|
||||||
$success
|
${success}
|
||||||
}
|
}
|
||||||
|
|
||||||
function listTrash() {
|
function listTrash() {
|
||||||
@@ -287,7 +287,7 @@ function trashAFile() {
|
|||||||
# Iterate over all files passed by user
|
# Iterate over all files passed by user
|
||||||
for userFile in "${args[@]}"; do
|
for userFile in "${args[@]}"; do
|
||||||
if [ ! -e "${userFile}" ]; then
|
if [ ! -e "${userFile}" ]; then
|
||||||
warning "$userFile: No such file or directory."
|
warning "${userFile}: No such file or directory."
|
||||||
continue
|
continue
|
||||||
fi
|
fi
|
||||||
# determine if we'll tell Finder to trash the file via AppleScript (very easy, plus free undo
|
# determine if we'll tell Finder to trash the file via AppleScript (very easy, plus free undo
|
||||||
@@ -295,36 +295,37 @@ function trashAFile() {
|
|||||||
if have_scriptable_finder; then
|
if have_scriptable_finder; then
|
||||||
# determine whether we have an absolute path name to the file or not
|
# determine whether we have an absolute path name to the file or not
|
||||||
if [ "${userFile:0:1}" = "/" ]; then
|
if [ "${userFile:0:1}" = "/" ]; then
|
||||||
file="$userFile"
|
file="${userFile}"
|
||||||
else
|
else
|
||||||
# expand relative to absolute path
|
# expand relative to absolute path
|
||||||
verbose "Determining absolute path for '$userFile'... "
|
verbose "Determining absolute path for '${userFile}'... "
|
||||||
file="$(realpath "$userFile")"
|
file="$(realpath "${userFile}")"
|
||||||
if [ $? -ne 0 ]; then
|
if [ $? -ne 0 ]; then
|
||||||
warning "Could not determine absolute path for '$userFile'!"
|
warning "Could not determine absolute path for '${userFile}'!"
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
verbose "Telling Finder to trash '$file'..."
|
verbose "Telling Finder to trash '${file}'..."
|
||||||
if /usr/bin/osascript -e "tell application \"Finder\" to delete POSIX file \"$file\"" &>/dev/null; then
|
if /usr/bin/osascript -e "tell application \"Finder\" to delete POSIX file \"$file\"" &>/dev/null; then
|
||||||
success "'$userFile' moved to trash"
|
#if osascript -e "tell app \"Finder\" to delete POSIX file \"${file}\"" &>/dev/null; then
|
||||||
|
success "'${userFile}' moved to trash"
|
||||||
else
|
else
|
||||||
warning "'$userFile' not moved to trash"
|
warning "'${userFile}' not moved to trash"
|
||||||
safeExit
|
safeExit
|
||||||
fi
|
fi
|
||||||
# Finder isn't available for this user, so don't rely on it (we'll do all the dirty work ourselves)
|
# Finder isn't available for this user, so don't rely on it (we'll do all the dirty work ourselves)
|
||||||
else
|
else
|
||||||
trash="/Users/${user}/.Trash/"
|
trash="/Users/${user}/.Trash/"
|
||||||
# create the trash folder if necessary
|
# create the trash folder if necessary
|
||||||
if [ ! -d "$trash" ]; then
|
if [ ! -d "${trash}" ]; then
|
||||||
mkdir $v "$trash"
|
mkdir $v "${trash}"
|
||||||
fi
|
fi
|
||||||
# move the file to the trash
|
# move the file to the trash
|
||||||
if [ ! -e "${trash}${userFile}" ]; then
|
if [ ! -e "${trash}${userFile}" ]; then
|
||||||
mv $v "$userFile" "$trash"
|
mv $v "${userFile}" "${trash}"
|
||||||
else
|
else
|
||||||
# determine if the filename has an extension
|
# determine if the filename has an extension
|
||||||
ext=false
|
ext=false
|
||||||
case "$userFile" in
|
case "${ }" in
|
||||||
*.*) ext=true ;;
|
*.*) ext=true ;;
|
||||||
esac
|
esac
|
||||||
|
|
||||||
@@ -436,10 +437,10 @@ while [[ $1 = -?* ]]; do
|
|||||||
-l|--list) list=true ;;
|
-l|--list) list=true ;;
|
||||||
-s|--secure) secureEmpty=true ;;
|
-s|--secure) secureEmpty=true ;;
|
||||||
-e|--empty) emptyTrash=true ;;
|
-e|--empty) emptyTrash=true ;;
|
||||||
-v|--verbose) verbose=1 ;;
|
-v|--verbose) verbose=true ;;
|
||||||
-q|--quiet) quiet=1 ;;
|
-q|--quiet) quiet=true ;;
|
||||||
-d|--debug) debug=1;;
|
-d|--debug) debug=true;;
|
||||||
--force) force=1 ;;
|
--force) force=true ;;
|
||||||
--endopts) shift; break ;;
|
--endopts) shift; break ;;
|
||||||
*) die "invalid option: '$1'." ;;
|
*) die "invalid option: '$1'." ;;
|
||||||
esac
|
esac
|
||||||
@@ -472,12 +473,12 @@ IFS=$'\n\t'
|
|||||||
set -o errexit
|
set -o errexit
|
||||||
|
|
||||||
# Run in debug mode, if set
|
# Run in debug mode, if set
|
||||||
if [ "${debug}" == "1" ]; then
|
if ${debug}; then
|
||||||
set -x
|
set -x
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Exit on empty variable
|
# Exit on empty variable
|
||||||
if [ "${strict}" == "1" ]; then
|
if ${strict}; then
|
||||||
set -o nounset
|
set -o nounset
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user