Created
November 8, 2021 10:50
-
-
Save KhaledLela/b2169723c4c7aef053be691e8b494487 to your computer and use it in GitHub Desktop.
SedAndAwk.sh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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