Skip to content

Instantly share code, notes, and snippets.

@juanvgarcia
Created October 30, 2013 06:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save juanvgarcia/7227899 to your computer and use it in GitHub Desktop.
Save juanvgarcia/7227899 to your computer and use it in GitHub Desktop.
This a very simple script that will comment host names depending on their initial numbering. My home network setup is different than my home's, and therefore I use this script to change the hostnames I use for development testing purposes.
#!/bin/sh
if [ "$1" != "work" ] && [ "$1" != "home" ];
then
echo "Wrong arguments Usage: $0 home|work"
exit 65
fi
work_test_regex="^10"
home_test_regex="^192"
work_off_regex="/^10/#10/g"
work_on_regex="/^#10/10/g"
home_off_regex="/^192/#192/g"
home_on_regex="/^#192/192/g"
if [ "$1" == "work" ];
then
work_test=`grep $work_test_regex /etc/hosts`
if [ -z "$work_test" ];
then
sed -i.bak "s$work_on_regex" /etc/hosts
sed -i.bak "s$home_off_regex" /etc/hosts
else
echo "Aborting switch to work mode since work mode seems to be activated."
exit 1
fi
else
home_test=`grep $home_test_regex /etc/hosts`
if [ -z "$home_test" ];
then
sed -i.bak "s$home_on_regex" /etc/hosts
sed -i.bak "s$work_off_regex" /etc/hosts
else
echo "Aborting switch to home mode since home mode seems to be activated."
exit 1
fi
fi
@livibetter
Copy link

Since `$work_test isn't used, it might be easier to read with just:

if ! grep -q $work_test_regex /etc/hosts;
then

A cleaner way to use if with exit status, just a small suggestion.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment