Skip to content

Instantly share code, notes, and snippets.

@RedL0tus RedL0tus/ayy.sh
Last active Dec 4, 2018

Embed
What would you like to do?
Let's replace f-words with blyat!
#!/bin/bash
# Ayy blyat!
# Replace all f-words with "blyat".
# set -x;
declare -A WORDS;
WORDS=( ["fuck"]="blyat"
["fucK"]="blyaT"
["fuCK"]="blyAT"
["fuCk"]="blyAt"
["fUCK"]="bLYAT"
["fUCk"]="bLYAt"
["fUck"]="bLYat"
["fUcK"]="bLYaT"
["FUCK"]="BLYAT"
["FUCk"]="BLYAt"
["FUck"]="BLYat"
["FUcK"]="BLYaT"
["FuCK"]="BlyAT"
["FuCk"]="BlyAt"
["Fuck"]="Blyat"
["FucK"]="BlyaT"
)
TMPDIR="/tmp/blyat";
# Usage info
if [ $# -lt 1 ]; then
echo ">>> No path given, quitting..."
echo ">>> Usage: ayy.sh <path1> [path2] [path3]"
exit 0;
else
echo ">>> Ayy!"
fi
# Generate sed pattern
PATTERN=""
for word in "${!WORDS[@]}"; do
PATTERN="$PATTERN s/$word/${WORDS[$word]}/g;"
done
echo ">>> sed pattern generated: $PATTERN"
# Create temp directory
if [ ! -d $TMPDIR ]; then
echo ">>> Creating temp directory...";
mkdir $TMPDIR;
fi
# Ayy
echo ">>> Start!"
for path in $@; do
if [ -e $path ]; then
echo ">>> Ayy $path";
BASENAME=$(basename $path);
REALPATH=$(realpath $path);
cp -R $REALPATH $TMPDIR/;
find $TMPDIR/$BASENAME -type f -exec sed -i -e "$PATTERN" {} \;;
pushd $TMPDIR > /dev/null;
ln -s $REALPATH $TMPDIR/$BASENAME-orig;
echo "# Generated by ayy.sh" > $BASENAME.patch;
diff -Naur $BASENAME-orig $BASENAME >> $BASENAME.patch;
unlink $BASENAME-orig;
popd > /dev/null;
cp -v $TMPDIR/$BASENAME.patch .;
else
echo ">>> $path not exsist, skipping..."
fi
done
# Clean up
echo ">>> Cleaning...";
rm -Rf $TMPDIR;
exit 0;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.