#! /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