#!/usr/bin/env bash # exit if a command fails set -o errexit # create function displaying usage usage() { echo -e "\\nUsage: wifi.sh connect [ssid] [psk]" echo -e " reconnect [ssid]" echo -e " scan" echo -e " known\\n" exit 1 } # make sure the correct number of arguments are passed; if not, output syntax and exit if [ "$#" -eq 0 ]; then usage fi # store the arguments as variables SSID="$2" PSK="$3" # check what command was requested and act accordingly if [ "$1" = "connect" ]; then # connect to the requested network nmcli device wifi connect "${SSID}" password "${PSK}" elif [ "$1" = "reconnect" ]; then # reconnect to the requested network nmcli connection up "${SSID}" elif [ "$1" = "scan" ]; then # scan for available networks nmcli dev wifi list elif [ "$1" = "known" ]; then # list known networks find /etc/NetworkManager/system-connections/*.nmconnection -type f -printf ' - %f\n' | sed 's/\.nmconnection//' else usage fi