Skip to content

Instantly share code, notes, and snippets.

@KhaledLela
Created November 8, 2021 10:50
Show Gist options
  • Save KhaledLela/b2169723c4c7aef053be691e8b494487 to your computer and use it in GitHub Desktop.
Save KhaledLela/b2169723c4c7aef053be691e8b494487 to your computer and use it in GitHub Desktop.
SedAndAwk.sh
#!/bin/bash
helpFunction()
{
echo ""
echo "Usage: $0 -d DOMAIN -n NAME"
echo "\t-d Root domain"
echo "\t-n Name/subdomain"
exit 1 # Exit script after printing help
}
while getopts ":d:n:" opt;
do
case "$opt" in
d ) DOMAIN="$OPTARG";;
n ) NAME="$OPTARG";;
? ) helpFunction ;; # Print helpFunction in case parameter is non-existent
esac
done
# Print helpFunction in case parameters are empty
if [ -z "$DOMAIN" ] || [ -z "$NAME" ]
then
echo "Missing params";
helpFunction
fi
# Make backup file
sudo cp $NAME.$DOMAIN $NAME.$DOMAIN.bak;
# Remove Line from file
sudo sed -ri '/# default lang sv/d' $NAME.$DOMAIN;
# remove lines from file with pattern
sudo sed -ri '/location ~?\s?\//,/.*\}/d' $NAME.$DOMAIN;
# Remove extra blank lines from file
sudo sed -ri ':a; /^\s*$/ {N;ba}; s/( *\n *){2,}/\n\n/' $NAME.$DOMAIN;
# Include lines at a specific location at file
sudo gawk -i inplace -v lineno=$line -v patt="include" -v text="\t# Include nk3 location config with regex handler.\n\tinclude /etc/nginx/conf/locations.conf;\n\n\t# expires handler from nginx config." ' NR > lineno && $0 ~ patt {print text} {print} ' $NAME.$DOMAIN;
# Remove block from the file by start and end
sed -ri '/ssl; # managed by Certbot.*$/,/ssl-dhparams.pem; # managed by Certbot/d' $NAME.$DOMAIN;
# append to file
echo "\n# Include nk3_maps that contains language variable mapping.\ninclude /etc/nginx/conf/nk3_maps.conf;" >> $NAME.$DOMAIN;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment