From 78d8d62332226c326575de341f613a29f3a98151 Mon Sep 17 00:00:00 2001 From: Chris Koeritz Date: Fri, 10 Apr 2015 20:17:03 -0400 Subject: [PATCH] tasty changes leading to a new alias called spacem that combines renlower and replace_spaces_with_underscores (previously called spaces_to_underscores). also some nice cleaning in safedel. --- scripts/core/functions.sh | 40 +++++++++++++- scripts/files/remove_here_if_not_there.sh | 55 +++++++++++++++++++ scripts/files/renlower.pl | 1 + ....sh => replace_spaces_with_underscores.sh} | 2 +- scripts/files/safedel.pl | 3 +- 5 files changed, 97 insertions(+), 4 deletions(-) create mode 100644 scripts/files/remove_here_if_not_there.sh rename scripts/files/{spaces_to_underscores.sh => replace_spaces_with_underscores.sh} (94%) diff --git a/scripts/core/functions.sh b/scripts/core/functions.sh index a7c0b627..ead36518 100644 --- a/scripts/core/functions.sh +++ b/scripts/core/functions.sh @@ -357,13 +357,13 @@ if [ -z "$skip_all" ]; then regenerate } +#uhhh, this does what now? function add_cygwin_drive_mounts() { for i in c d e f g h q z ; do ln -s /cygdrive/$i $i done } - # takes a file to modify, and then it will replace any occurrences of the # pattern provided as the second parameter with the text in the third # parameter. @@ -380,6 +380,44 @@ if [ -z "$skip_all" ]; then sed -i -e "s%$pattern%$replacement%g" "$file" } + function spacem() + { +#hmmm: could really use that pattern of 'iterate across all the arguments and do same thing' here. + +#hmmm: it actually seems like the below IS the pattern. it's pretty short, but i wish it could be shorter, like a function in itself.... ah. +# couldn't we have a functionator deal that takes: +# 1) a command to run, and +# 2-n) arguments, +# where the function just blithely runs that command on all of those arguments!? +# yes! that does seem like the pattern being sought, much nicer than the goofy loop below, +# although this functionator deal needs to handle when there are more than one command also, +# or this very function couldn't be implemented... +# maybe a two step process: +# 1) build a list of commands to run on all arguments, +# 2) then run through all the arguments passed in using those established commands. +# yes again! this seems like it would meet all the needs involved and not be too irksome. +# for the example below, this would reduce the number of lines, i think. +# i had better stop bloviating and write this function so i can determine the number of +# lines omitted by the new approach. + while [ $# -gt 0 ]; do + arg="$1"; shift + # first we rename the file to be lower case. + perl $FEISTY_MEOW_SCRIPTS/files/renlower.pl "$arg" &>/dev/null + # oops, now the name is all lower-case. we need to make the + # same adjustment. + arg2="$(echo "$arg" | tr A-Z a-z)" + # we definitely wanted to adjust the case first, rather than doing all + # the wacky stuff this script does to the filename... we will capture + # the output of the replace operaton for reporting. + final_name="$(perl "$FEISTY_MEOW_SCRIPTS/files/replace_spaces_with_underscores.sh" "$arg2")" + # now zap the intermediate part of the name off. + final_name="$(echo \"$final_name\" | sed -e 's/.*=> //')" + # printout the combined operation results. + echo "'$arg' => $final_name" + done +# + } + ############## function function_sentinel() { return 0; } diff --git a/scripts/files/remove_here_if_not_there.sh b/scripts/files/remove_here_if_not_there.sh new file mode 100644 index 00000000..8e78600e --- /dev/null +++ b/scripts/files/remove_here_if_not_there.sh @@ -0,0 +1,55 @@ +#!/bin/bash + +function print_instructions() +{ + echo -e "\n$(basename $0 .sh):\n" + echo -e 'this script takes two parameters, a "here" folder and a "there" folder, almost as if it were a copy command. but instead, this removes any file from under the "here" location if it cannot be found in the "there" location. so the "there" location is considered a more definitive template of what should be in "here", such that we strip out what "there" does not have.\n\n +the most" useful way to use this script is for a "here" hierarchy that is a copy of an older version of another "there" hierarchy. the "there" hierarchy may have changed a lot, including new files, changed files, and deleted files. it is a simple operation to copy everything from "there" into "here" (such as by using the command [ cp -R "$there"/* "$here" ] ) , but it is a lot harder to determine what stuff in "here" is out of date and should be removed. that is where this script comes in; it can be run to flush out any older things in "here", rather than requiring the user to manually find all those files. ' | splitter + echo + echo "Example Usage:" + echo + echo "$(basename $0 .sh) backup_copy original_folder" + echo + echo " ==> Deletes any files in backup_copy that are not found in original_folder." + echo +} + +if [ $# -lt 2 ]; then + print_instructions + echo "There were not enough parameters provided to the script." + exit 1 +fi + +here="$1"; shift +there="$1"; shift + +if [ ! -d "$there" -o ! -d "$here" ]; then + print_instructions + echo "One of the directories specified does not exist." + exit 1 +fi + +here_temp_file="$(mktemp "$TMP/remover_list.XXXXXX")" +there_temp_file="$(mktemp "$TMP/remover_list.XXXXXX")" + +# find all the files in both hierarchies. +pushd "$here" &>/dev/null +find "." -type f | sort >"$here_temp_file" +popd &>/dev/null +pushd "$there" &>/dev/null +find "." -type f | sort >"$there_temp_file" +popd &>/dev/null + +whack_list="$(mktemp "$TMP/remover_list.XXXXXX")" + +comm -23 "$here_temp_file" "$there_temp_file" >"$whack_list" + +while read line; do + if [ -z "$line" ]; then break; fi + herename="$here/$line" + rm -v "$herename" +done <"$whack_list" + +# clean up. +rm "$whack_list" "$here_temp_file" "$there_temp_file" + diff --git a/scripts/files/renlower.pl b/scripts/files/renlower.pl index a45c338d..7f9f3409 100644 --- a/scripts/files/renlower.pl +++ b/scripts/files/renlower.pl @@ -49,6 +49,7 @@ sub rename_lower { || die "failed to do initial rename"; rename($intermediate_name, $new_name) || die "failed to do secondary rename"; + print "'$old_name' => '$new_name'\n"; } } } diff --git a/scripts/files/spaces_to_underscores.sh b/scripts/files/replace_spaces_with_underscores.sh similarity index 94% rename from scripts/files/spaces_to_underscores.sh rename to scripts/files/replace_spaces_with_underscores.sh index 03c58cfc..375b8274 100644 --- a/scripts/files/spaces_to_underscores.sh +++ b/scripts/files/replace_spaces_with_underscores.sh @@ -15,7 +15,7 @@ while [ $# -gt 0 ]; do newname="$(echo "$file" | tr -s ' ' '_' | tr -d "}{)(][\\\~',:?><\"" | sed -e 's/\([0-9]\)_\./\1./g' | sed -e 's/_-_/-/' )" if [ "$file" != "$newname" ]; then # we've effected a name change, so let's actually do it. - echo "moving '$file' => '$newname' " + echo "'$file' => '$newname'" mv "$file" "$newname" fi done diff --git a/scripts/files/safedel.pl b/scripts/files/safedel.pl index e9de4046..60408046 100644 --- a/scripts/files/safedel.pl +++ b/scripts/files/safedel.pl @@ -89,7 +89,6 @@ sub safedel { # reset the list of objects actually whacked. local(@deleted) = (); -# print "deleted list is @deleted\n"; # iterate over the files that we have been told to nuke. foreach $file (@to_delete) { @@ -107,7 +106,7 @@ sub safedel { $date_tool = "date"; local($datestamp) = `$date_tool +%Y-%m-%d-%H%M`; while ($datestamp =~ /[\r\n]$/) { chop $datestamp; } - $archive_file = $temp_subdir . "/deleted-#$number-" . $datestamp; + $archive_file = $temp_subdir . "/del-$number-" . $datestamp; #print "archive_file is $archive_file; file is $file.\n"; if (-d $file) { -- 2.34.1