bash: replace sar with sr
All checks were successful
ci/woodpecker/push/lint Pipeline was successful

Signed-off-by: Michael Grote <michael.grote@dataport.de>

add new sr script

Signed-off-by: Michael Grote <michael.grote@dataport.de>
This commit is contained in:
Michael Grote 2024-05-29 14:01:48 +02:00 committed by Michael Grote
parent 8f2deeb484
commit 51da6fdf67
2 changed files with 79 additions and 28 deletions

View file

@ -144,31 +144,3 @@ ghist() {
esac esac
done done
} }
function sar {
case "$1" in
--help | -h)
cat <<EOF
Description:
Search and replace recursively.
Usage:
sar [--help] <search> <replace>
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
}

79
scripts/sr Executable file
View file

@ -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 <<EOF
Description:
Search and replace recursively.
Usage:
sr [-h|-d|-i|-f] <search> <replace>
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