From df9b1e5be6c370ba16c6a790c713f9467cf5a002 Mon Sep 17 00:00:00 2001 From: Enrique Benitez Date: Sun, 7 May 2017 22:20:23 -0500 Subject: [PATCH] spaces in folder names support - fix subshell issue: not updating modules_removed global variable --- wipe-modules.sh | 84 +++++++++++++++++++++++++++---------------------- 1 file changed, 47 insertions(+), 37 deletions(-) diff --git a/wipe-modules.sh b/wipe-modules.sh index 038b369..10b162d 100644 --- a/wipe-modules.sh +++ b/wipe-modules.sh @@ -32,47 +32,32 @@ usage() { EOF } -# instructs agent gir to start ripping off those pesky node_modules -go_gir() { - # move to code directory - cd $code_dir - - # if $dry is --dry (or -D) then show message - if [ "$dry" == "--dry" -o "$dry" == "-D" ]; then - echo "\033[90m The node_modules in the following directories can be wiped out. \033[39m" - sleep 2 - echo "" - fi - - # find the code directories whose last modify time (mtime) is older than $last_modified days - # and loop through each resulting dir - find . -maxdepth 1 -type d -mtime +$last_modified | while read d; do - if [ "$d" == "." ]; then - continue - fi - # move to code dir subdirectory - cd "$d" - if [ `find . -maxdepth 1 -type d -name 'node_modules'` ] +# wipe node_modules +wipe() { + dir="$1" # now dir is the 1st parameter ($1) + dir="${dir%/}" # strip trailing slash + dir="${dir##*/}" # strip path and leading slash + cd "$dir" + if [ $(find . -maxdepth 1 -type d -name 'node_modules') ]; then + # if $dry is not --dry (or -D) then just print the name of the folder that + # matches the search + if [ "$dry" == "--dry" -o "$dry" == "-D" ] then - # if $dry is not --dry (or -D) then just print the name of the folder that - # matches the search - if [ "$dry" == "--dry" -o "$dry" == "-D" ] - then - # print the current directory name - echo "$d" - modules_removed=`expr $modules_removed + 1` - else - # wipe the node_modules folder! - rm -rf node_modules - modules_removed=`expr $modules_removed + 1` - fi + # print the current directory name + echo "\033[90m $dir \033[39m" + else + # wipe the node_modules folder! + rm -rf node_modules fi - # go one directory up to continue iterating - cd .. - done + # counter for modules to be removed + modules_removed=$(expr $modules_removed + 1) + fi + cd .. +} - # display nice little information message when done +# exit message +display_message() { if [ $modules_removed -gt 0 ]; then if [ "$dry" == "--dry" -o "$dry" == "-D" ]; then echo "" @@ -85,6 +70,31 @@ go_gir() { fi } +# instructs agent gir to start ripping off those pesky node_modules +go_gir() { + # move to code directory + cd $code_dir + + # if $dry is --dry (or -D) then show message + if [ "$dry" == "--dry" -o "$dry" == "-D" ]; then + echo "\033[90m The node_modules in the following directories can be wiped out: \033[39m" + sleep 2 + echo "" + fi + + # find $code_dir directories whose last modify time (mtime) is older than + # $last_modified days, loop through each resulting dir and execute wipe function + # then display message + find . -maxdepth 1 -type d -mtime +$last_modified | + { + while read d; do + wipe "$d" + done + + display_message + } +} + # if $1 parameter is --help or -h then show usage info if [ "$1" == "--help" -o "$1" == "-h" ] then