diff --git a/.bash_functions b/.bash_functions index e51d73f..95df334 100644 --- a/.bash_functions +++ b/.bash_functions @@ -144,31 +144,3 @@ ghist() { esac done } -function sar { - case "$1" in - --help | -h) - cat < - -Options: --h, --help Print this help. - -Author: -michael.grote@posteo.de - git.mgrote.net -EOF -;; -esac -if [ "$1" != "-h" ] && [ "$1" != "--help" ] ; then - if [ $# = 2 ] ; then - find . -name '*' -type f -not -path '*/\.git/*' -exec sed -i "s/$1/$2/" {} \; - elif [ $# = 1 ] ; then - echo ">>> Not enough arguments..." - elif [ $# -gt 2 ] ; then - echo ">>> Too many arguments..." - fi -fi -} diff --git a/scripts/sr b/scripts/sr new file mode 100755 index 0000000..eceb29e --- /dev/null +++ b/scripts/sr @@ -0,0 +1,79 @@ +#!/bin/bash + +function rename_directories { + find . -type d -not -path '*/\.git/*' -execdir rename "$SEARCH" "$REPLACE" {} \; > /dev/null 2>&1 +} + +function rename_files { + find . -name '*' -type f -not -path '*/\.git/*' -exec rename "$SEARCH" "$REPLACE" {} \; +} + +function rename_inline { + find . -name '*' -type f -not -path '*/\.git/*' -exec sed -i "s/${SEARCH}/${REPLACE}/g" {} \; +} + +# das erste Argument der Funktion ist die Anzahl der Argumente des aufrufenden Scripts +# das zweite Argument ist die erwünschte Anzahl an Argumenten +function check_argument_count { + if [ "$1" -lt "$2" ] ; then + echo ">>> Not enough arguments..." + exit 1 + elif [ "$1" -gt "$2" ] ; then + echo ">>> Too many arguments..." + exit 2 + fi +} + +function print_help { +cat < + +Options: +-d Rename only directories. +-f Rename only files. +-i Rename only in files. +-h Print this help. + +Author: +michael.grote@posteo.de - git.mgrote.net +EOF +} + +SEARCH="$2" +REPLACE="$3" + +case "$1" in + -f) + check_argument_count "$#" "3" + rename_files + ;; + -i) + check_argument_count "$#" "3" + rename_inline + ;; + -d) + check_argument_count "$#" "3" + rename_directories > /dev/null 2>&1 + ;; + -h) + print_help + exit 0 + ;; + # ohne Parameter wird alles gemacht + *) + check_argument_count "$#" "2" + SEARCH=$1 + REPLACE=$2 + echo ">>> Rename files..." + rename_files + echo ">>> Rename directories..." + rename_directories > /dev/null 2>&1 + echo ">>> Rename inline..." + rename_inline + ;; +esac