Browse Source

Valid input checking + organization

Added valid input checking on:

* DHCP or Static? choice
* interface selection

Moved choice to own function, moved conshow to setstatic and setdynamic
master
Brandon Nolet 2 years ago
parent
commit
4696169610
1 changed files with 46 additions and 12 deletions
  1. +46
    -12
      nmcli-basic.sh

+ 46
- 12
nmcli-basic.sh View File

@@ -23,7 +23,10 @@ function usage() {

NMCLI_BIN=$(which nmcli)



function setstatic() {
conshow

#Getting Static IP info
read -p "Enter the desired IPv4 address: " IPAdd
@@ -46,6 +49,8 @@ function setstatic() {
}

function setdynamic() {
conshow

echo "Configuring..."
nmcli con mod ${IntID} ipv4.method auto

@@ -63,19 +68,57 @@ function apply() {
}

function conshow() {
${NMCLI_BIN} con show

echo
read -p "Which interface to configure? " IntID
if [[ ${IntID} != '' ]]; then #empty strings make grep have a fit
nmcli -t con show | cut -d : -f 1 | grep $IntID &> /dev/null
if [[ $? != 0 ]]; then #checking for valid interface name being input
echo
echo "Sorry, that's not the name of a valid interface, try again"
echo
sleep 1
conshow #would love to have a non-inception like way to do this.
else
return 0 #If the input matches the name of an interface, move on
fi
else #empty string is empty, prompt again
echo
echo "Sorry, that's not the name of a valid interface, try again"
echo
sleep 1
conshow
fi
echo

}

function getchoice() {
read -p "Static or DHCP? " CHOICE
case ${CHOICE} in
[dD]ynamic | [aA]uto | [aA]utomatic | [dD][hH][cC][pP])
setdynamic
;;
[sS]tatic | [mM]anual)
setstatic
;;
*)
"Sorry, that's not a valid choice"
sleep 1
getchoice
;;
esac
}

case "$1" in
[dD]ynamic | [aA]uto | [aA]utomatic | [dD][hH][cC][pP])
conshow
setdynamic
;;
[sS]tatic | [mM]anual)
conshow
setstatic
;;

@@ -83,16 +126,7 @@ case "$1" in
usage
;;
"")
read -p "Static or DHCP? " CHOICE
case ${CHOICE} in
[dD]ynamic | [aA]uto | [aA]utomatic | [dD][hH][cC][pP])
setdynamic
;;
[sS]tatic | [mM]anual)
setstatic
;;
esac
;;
*)
echo "Sorry, argument '${1}' not understood."
echo "Try passing 'usage' instead"


Loading…
Cancel
Save