Skip to content

Instantly share code, notes, and snippets.

@signed8bit
Created May 2, 2016 13:27
Show Gist options
  • Save signed8bit/3da09a6778d94686470f6e41dee6f9cc to your computer and use it in GitHub Desktop.
Save signed8bit/3da09a6778d94686470f6e41dee6f9cc to your computer and use it in GitHub Desktop.
A simple wrapper script for setting a network location on OS X using the networksetup command.
#! /usr/bin/env bash
##
# Switches to the specified network location
##
function switch_location() {
if ! /usr/sbin/networksetup -switchtolocation "${loc}" >/dev/null 2>&1 ; then
echo "Error: Network location \"${loc}\" not found"
fi
}
##
# Lists the available network locations
##
function list_locations() {
networksetup -listlocations
}
##
# Prints usage information
##
function usage() {
echo "Usage: netloc [-l] [-h] location"
echo "Example: netloc work"
}
##
# Parse options
##
while getopts ':lh' opt; do
case ${opt} in
l)
list_locations
exit 0
;;
h)
usage
exit 0
;;
\?)
echo "Error: -$OPTARG is an invalid option" >&2
usage
exit 1
;;
:)
echo "Error: -$OPTARG requires an argument" >&2
usage
exit 1
;;
esac
done
##
# Parses the required operand
##
shift $((OPTIND-1))
if [[ $# -lt 1 ]]; then
usage
exit 1
fi
##
# Must be run as root or via sudo
##
if [ "$EUID" -ne 0 ]; then
echo "Error: Must be run as the superuser"
exit 1
fi
loc=${1}
switch_location
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment