bugfix, esthetic cleanup, better comments, version bump
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
#!/usr/bin/env bash
|
||||
# ------------------------------------------------------------------------------
|
||||
# Copyright (c) 2013-2022 Geoffray Levasseur <fatalerrors@geoffray-levasseur.org>
|
||||
# Protected by the BSD3 license. Please read bellow for details.
|
||||
@@ -47,49 +48,48 @@ expandlist()
|
||||
echo $result
|
||||
}
|
||||
|
||||
|
||||
# ------------------------------------------------------------------------------
|
||||
# Clean a directory or a tree from temporary or backup files
|
||||
# ------------------------------------------------------------------------------
|
||||
clean ()
|
||||
clean()
|
||||
{
|
||||
for opt in $@ ; do
|
||||
for opt in $@; do
|
||||
case $opt in
|
||||
"-r"|"--recurs")
|
||||
local recursive=1
|
||||
;;
|
||||
"-r" | "--recurs")
|
||||
local recursive=1
|
||||
;;
|
||||
|
||||
"-h"|"--help")
|
||||
echo "clean: erase backup files in the given directories."
|
||||
echo
|
||||
echo "Usage: clean [option] [directory1] [...[directoryX]]"
|
||||
echo
|
||||
echo "Options:"
|
||||
echo " -h, --help Display that help screen"
|
||||
echo " -r, --recurs Do a recursive cleaning"
|
||||
echo " -f, --force Do not ask for confirmation (use with care)"
|
||||
echo " -s, --shell Do nothing and display what will be executed"
|
||||
echo
|
||||
return 0
|
||||
;;
|
||||
"-h" | "--help")
|
||||
echo "clean: erase backup files in the given directories."
|
||||
echo
|
||||
echo "Usage: clean [option] [directory1] [...[directoryX]]"
|
||||
echo
|
||||
echo "Options:"
|
||||
echo " -h, --help Display that help screen"
|
||||
echo " -r, --recurs Do a recursive cleaning"
|
||||
echo " -f, --force Do not ask for confirmation (use with care)"
|
||||
echo " -s, --shell Do nothing and display what will be executed"
|
||||
echo
|
||||
return 0
|
||||
;;
|
||||
|
||||
"-s"|"--shell")
|
||||
local outshell=1
|
||||
;;
|
||||
"-s" | "--shell")
|
||||
local outshell=1
|
||||
;;
|
||||
|
||||
"-f"|"--force")
|
||||
local force=1
|
||||
;;
|
||||
"-f" | "--force")
|
||||
local force=1
|
||||
;;
|
||||
|
||||
"-"*)
|
||||
disp E "Invalid option, use \"clean --help\" to display usage."
|
||||
echo
|
||||
return 1
|
||||
;;
|
||||
"-"*)
|
||||
disp E "Invalid option, use \"clean --help\" to display usage."
|
||||
echo
|
||||
return 1
|
||||
;;
|
||||
|
||||
*)
|
||||
local dirlist="$dirlist $opt"
|
||||
;;
|
||||
*)
|
||||
local dirlist="$dirlist $opt"
|
||||
;;
|
||||
esac
|
||||
done
|
||||
|
||||
@@ -100,7 +100,7 @@ clean ()
|
||||
unset recursive force
|
||||
|
||||
for dir in $dirlist; do
|
||||
local dellist=$(find $dir $findopt -type f -name "*~" -o -name "#*#" \
|
||||
local dellist=$(find "$dir" $findopt -type f -name "*~" -o -name "#*#" \
|
||||
-o -name "*.bak" -o -name ".~*#")
|
||||
for f in $dellist; do
|
||||
if [[ ! $outshell ]]; then
|
||||
@@ -114,73 +114,71 @@ clean ()
|
||||
}
|
||||
export -f clean
|
||||
|
||||
|
||||
# ------------------------------------------------------------------------------
|
||||
# Create a directory then goes inside
|
||||
# ------------------------------------------------------------------------------
|
||||
mcd ()
|
||||
mcd()
|
||||
{
|
||||
if [[ ! $# -eq 1 ]] ; then
|
||||
if [[ ! $# -eq 1 ]]; then
|
||||
disp E "Create a directory then goes inside."
|
||||
disp E "Usage: mcd <directory>"
|
||||
return 1
|
||||
fi
|
||||
mkdir -pv $1 && cd $1
|
||||
mkdir -pv "$1" && cd "$1" || echo "Failed create or change directory."
|
||||
}
|
||||
export -f mcd
|
||||
|
||||
|
||||
# ------------------------------------------------------------------------------
|
||||
# Rename all files in current directory to replace spaces with _
|
||||
# ------------------------------------------------------------------------------
|
||||
rmspc ()
|
||||
rmspc()
|
||||
{
|
||||
local lst=""
|
||||
for opt in $@ ; do
|
||||
for opt in $@; do
|
||||
case $opt in
|
||||
"-h"|"--help")
|
||||
echo "rmspc: remove spaces from all filenames in current directories"
|
||||
echo
|
||||
echo "Usage: rmspc [option]"
|
||||
echo
|
||||
echo "Options:"
|
||||
echo " -h, --help Display that help screen"
|
||||
echo " -r, --recursive Treat subdirectories of the given directory"
|
||||
echo " -c, --subst-char Change the replacement character (default is underscore)"
|
||||
echo " -v, --verbose Display more details (recursive mode only)"
|
||||
echo " -s, --shell Do nothing and display commands that would be executed"
|
||||
echo
|
||||
echo "Note: if the --subst-char option is given without parameters, spaces will be"
|
||||
echo " replaced with nothing (concatenation)."
|
||||
echo
|
||||
return 0
|
||||
;;
|
||||
"-h" | "--help")
|
||||
echo "rmspc: remove spaces from all filenames in current directories"
|
||||
echo
|
||||
echo "Usage: rmspc [option]"
|
||||
echo
|
||||
echo "Options:"
|
||||
echo " -h, --help Display that help screen"
|
||||
echo " -r, --recursive Treat subdirectories of the given directory"
|
||||
echo " -c, --subst-char Change the replacement character (default is underscore)"
|
||||
echo " -v, --verbose Display more details (recursive mode only)"
|
||||
echo " -s, --shell Do nothing and display commands that would be executed"
|
||||
echo
|
||||
echo "Note: if the --subst-char option is given without parameters, spaces will be"
|
||||
echo " replaced with nothing (concatenation)."
|
||||
echo
|
||||
return 0
|
||||
;;
|
||||
|
||||
"-r"|"--recursive")
|
||||
local recurs=1
|
||||
;;
|
||||
"-r" | "--recursive")
|
||||
local recurs=1
|
||||
;;
|
||||
|
||||
"-c"?*|"--subst-char"?*)
|
||||
if [[ $(echo $opt | grep "=") ]]; then
|
||||
local substchar=$(echo "$opt" | cut -f 2- -d '=')
|
||||
else
|
||||
local substchar='none'
|
||||
fi
|
||||
;;
|
||||
"-c"?* | "--subst-char"?*)
|
||||
if [[ $(echo $opt | grep "=") ]]; then
|
||||
local substchar=$(echo "$opt" | cut -f 2- -d '=')
|
||||
else
|
||||
local substchar='none'
|
||||
fi
|
||||
;;
|
||||
|
||||
"-v"|"--verbose")
|
||||
local verb=1
|
||||
;;
|
||||
"-v" | "--verbose")
|
||||
local verb=1
|
||||
;;
|
||||
|
||||
"-s"|"--shell")
|
||||
local shell=1
|
||||
;;
|
||||
"-s" | "--shell")
|
||||
local shell=1
|
||||
;;
|
||||
|
||||
*)
|
||||
disp E "Invalid parameter, use \"rmspc --help\" to display options list"
|
||||
echo
|
||||
return 1
|
||||
;;
|
||||
*)
|
||||
disp E "Invalid parameter, use \"rmspc --help\" to display options list"
|
||||
echo
|
||||
return 1
|
||||
;;
|
||||
esac
|
||||
done
|
||||
|
||||
@@ -191,14 +189,14 @@ rmspc ()
|
||||
for f in *; do
|
||||
[[ $recurs ]] && [[ -d "$f" ]] && (
|
||||
[[ $verb ]] && disp I "Entering directory $(pwd)/$f ..."
|
||||
local lastdir=$f
|
||||
pushd "$f" > /dev/null
|
||||
local lastdir=$f
|
||||
pushd "$f" >/dev/null
|
||||
rmspc $@
|
||||
popd > /dev/null
|
||||
popd >/dev/null
|
||||
[[ $verb ]] && disp I "Leaving directory $(pwd)/$lastdir"
|
||||
unset lastdir
|
||||
unset lastdir
|
||||
)
|
||||
|
||||
|
||||
if [[ $(echo $f | grep " ") ]]; then
|
||||
local newf="${f// /${substchar}}"
|
||||
local command="mv $mvopt \"$f\" \"$newf\""
|
||||
@@ -209,6 +207,9 @@ rmspc ()
|
||||
fi
|
||||
fi
|
||||
done
|
||||
unset lst substchar verb shell newf command mvopt
|
||||
unset lst substchar verb shell newf command mvopt
|
||||
}
|
||||
export -f rmspc
|
||||
|
||||
# ------------------------------------------------------------------------------
|
||||
# EOF
|
||||
|
||||
Reference in New Issue
Block a user