diff options
author | Jakub Jankowski <shasta@toxcorp.com> | 2017-11-21 02:38:53 +0100 |
---|---|---|
committer | Robby Workman <rworkman@slackware.com> | 2017-11-21 00:07:59 -0600 |
commit | 9a51af88aa234e2ceb6a1daf830f63a11202beb3 (patch) | |
tree | c6efa8ebdcfc82c4ddebe28cb2445ed69763c7ca /rc.inet1 | |
parent | 230e46830f5d6915c8203c6e75497973ac48bc8c (diff) | |
download | slacknetsetup-9a51af88aa234e2ceb6a1daf830f63a11202beb3.tar.xz |
rc.inet1*: Simplify virtif_* code, add note to example config
There is a built-in way of counting the number of elements in an array:
${#arrayname[@]}. Use this, and the fact that "for i in $(seq 0 -1)" will
not evaluate body of the loop even once, to simplify code and get rid of
virtif_determine().
While at it, add a note to rc.inet1.conf mentioning the necessity of
keeping array indexes monotonically increasing (from 0), otherwise the
code will fail (so would the original code).
Signed-off-by: Robby Workman <rworkman@slackware.com>
Diffstat (limited to 'rc.inet1')
-rw-r--r-- | rc.inet1 | 23 |
1 files changed, 8 insertions, 15 deletions
@@ -73,27 +73,20 @@ lo_down() { # INTERFACE FUNCTIONS # ####################### -# Function to determine if virtual interfaces are defined -virtif_determine() { - if [ -n "${VIRTIFNAME}" ]; then - virtifcount=$(($(echo ${VIRTIFNAME[@]} | wc -w) - 1)) - else - return 1 - fi -} - # Function to create virtual interfaces virtif_create() { - # argument is 'i' - the position of this interface in the IFNAME array. - for i in $(seq 0 ${virtifcount}); do + # argument is 'i' - the position of this interface in the VIRTIFNAME array. + # this loop goes from i=0 to i=number_of_configured_virtual_interfaces_minus_one + # which means it doesn't do anything if there are none. + for i in $(seq 0 $((${#VIRTIFNAME[@]} - 1))); do /sbin/ip tuntap add dev ${VIRTIFNAME[$i]} mode ${VIRTIFTYPE[$i]} user ${VIRTIFUSER[$i]} group ${VIRTIFGROUP[$i]} done } # Function to destory virtual interfaces virtif_destroy() { - # argument is 'i' - the position of this interface in the IFNAME array. - for i in $(seq 0 ${virtifcount}); do + # argument is 'i' - the position of this interface in the VIRTIFNAME array. + for i in $(seq 0 $((${#VIRTIFNAME[@]} - 1))); do /sbin/ip tuntap del dev ${VIRTIFNAME[$i]} mode ${VIRTIFTYPE[$i]} done } @@ -296,7 +289,7 @@ gateway_down() { # Function to start the network: start() { lo_up - virtif_determine && virtif_create + virtif_create for i in "${IFNAME[@]}" ; do if_up $i done @@ -309,7 +302,7 @@ stop() { for i in "${IFNAME[@]}" ; do if_down $i done - virtif_determine && virtif_destroy + virtif_destroy lo_down } |