Skip to content

Instantly share code, notes, and snippets.

@basz
Created March 22, 2018 14:23
Show Gist options
  • Select an option

  • Save basz/5e0c49e1fde07147347c7b2d1a6ba869 to your computer and use it in GitHub Desktop.

Select an option

Save basz/5e0c49e1fde07147347c7b2d1a6ba869 to your computer and use it in GitHub Desktop.
flush cache
#!/usr/bin/env bash
# Copyright 2018 Bushbaby Multimedia
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.
# see http://coolestguidesontheplanet.com/clear-the-local-dns-cache-in-osx/
vercomp () {
if [[ $1 == $2 ]]
then
return 0
fi
local IFS=.
local i ver1=($1) ver2=($2)
# fill empty fields in ver1 with zeros
for ((i=${#ver1[@]}; i<${#ver2[@]}; i++))
do
ver1[i]=0
done
for ((i=0; i<${#ver1[@]}; i++))
do
if [[ -z ${ver2[i]} ]]
then
# fill empty fields in ver2 with zeros
ver2[i]=0
fi
if ((10#${ver1[i]} > 10#${ver2[i]}))
then
return 1
fi
if ((10#${ver1[i]} < 10#${ver2[i]}))
then
return 2
fi
done
return 0
}
version_compare () {
VERSION=$(sw_vers -productVersion)
vercomp $VERSION $1;
case $? in
0) op='=';;
1) op='>';;
2) op='<';;
esac
if [ $2 = '=' ] && [ $op = '=' ]; then
return 0
fi
if [ $2 = '>=' ] && [ $op != '<' ]; then
return 0
fi
if [ $2 = '>' ] && [ $op = '>' ]; then
return 0
fi
if [ $2 = '<=' ] && [ $op != '>' ]; then
return 0
fi
if [ $2 = '<' ] && [ $op = '<' ]; then
return 0
fi
return 1
}
if $(version_compare '10.10.4' '>='); then
echo '10.10.4+'
sudo killall -HUP mDNSResponder
exit 0
fi
if $(version_compare '10.10' '>='); then
echo '10.10+'
sudo discoveryutil mdnsflushcache
exit 0
fi
if $(version_compare '10.7' '>='); then
echo '10.7+'
sudo killall -HUP mDNSResponder
exit 0
fi
if $(version_compare '10.5' '>='); then
echo '10.5+'
sudo dscacheutil -flushcache
exit 0
fi
echo "unsupported os version";
exit 1;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment