Skip to content

Instantly share code, notes, and snippets.

@Aricg
Last active December 11, 2015 23:19
Show Gist options
  • Save Aricg/4676074 to your computer and use it in GitHub Desktop.
Save Aricg/4676074 to your computer and use it in GitHub Desktop.
#!/bin/bash
#set -e
allfiles=(/root/work4/xi_wang);
#echo "I count ${#allfiles[@]} afiles to manipulate."
for afile in ${allfiles[@]};
do
virtualhostcount=0
#... I should use awk rather than grep..
vhost=($(cat ${afile} | grep "ServerName"| awk {'print $2'} | grep -v "#" ))
actualvhosts=$(cat ${afile} | grep ServerName | wc -l)
#check if my patten matching agrees with a simple line count, if not exit.
if (( ${#vhost[@]} != $actualvhosts )); then
echo "edge case detected probably due to my shitty grep/awk that determins server name."
exit 1
fi
echo "Filename: ------ ${afile}"
echo "Nuber of vhosts: ${#vhost[@]} "
echo " "
while ((virtualhostcount < ${#vhost[@]}))
do
awk -v n=0 -v "f=$virtualhostcount" -v "vhost=${vhost[$virtualhostcount]}" '/<VirtualHost\ \*:80/{if (n == f ) print "NameVirtualHost *:80 " ; n++ } {print} ' $afile > tmpfile.txt && mv tmpfile.txt $afile
((virtualhostcount++))
done
done
#Or if you are sluggr ... printf '%s\n' 'g/ServerName/t?<VirtualHost?-\' 's/^[[:blank:]]*ServerName/NameVirtualHost/' %p | ed -s file
~
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment