bash: sr, create symlink

Signed-off-by: Michael Grote <michael.grote@posteo.de>
This commit is contained in:
Michael Grote 2024-06-05 20:33:18 +02:00
parent 873764d369
commit dcdfcd7cb4
2 changed files with 80 additions and 79 deletions

79
scripts/search_and_replace 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

View file

@ -1,79 +0,0 @@
#!/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

1
scripts/sr Symbolic link
View file

@ -0,0 +1 @@
search_and_replace