0

I tried changing some things in my code and still it doesn't work :S

#!/bin/bash 



function auto_net() {
    welcome
}

function welcome() {    
    echo "Will your net include a server?"
    read choice 
    if [ $choice -eq "Yes" ]; then
        interface Server;   
    elif [ $choice -eq "No" ];
    then
        interface Default;
    fi
}

function user_input() {
    declare -a devices=("${!1}")
    for((i=1; i <= $2; ++i)); do
         device=$3
         device="$device$i"
         devices+=("$device")   
    done
    for i in ${devices[@]}; do
        echo $i
    done
}

function interface() {
    if[ "$1" -eq "Server" ];
    then
        set_up Server;
        set_up Router;
        set_up Switch;
        set_up Host;
    elif[ "$1" -eq "Default" ]; then
        set_up Router;
        set_up Switch;
        set_up Host;
    fi
}

function set_up (){
    local routers=()
    local hosts=()      
    local switches=()
    if [ "$1" -eq  "Router" ];
    then    
        echo "How many routers are you going to configure?"
        read router_number
        user_input routers[@] router_number R 
    elif [ "$1" -eq "Switch" ];
    then
        echo "How many switches are there?"
        read switch_number
        user_input switches[@] switch_number S 

       elif [ $1 -eq "Host" ];
       then
            echo "How many hosts per switch? Type it in order"
        read host_number
        user_input hosts[@] host_number H
       fi 
}

auto_net
echo $?
1

1 Answer 1

5

You are missing some spaces.

  • if[ "$1" -eq "Server" ];
  • elif[ "$1" -eq "Default" ]; then

These should be a space after the keywords if and elif.

  • for((i=1; i <= $2; ++i)); do

This (missing space after for) is not strictly an error, but should be fixed for consistency with the rest of the code.

You are also using -eq throughout for string comparisons. To compare strings, use = (-eq compares integers):

if [ $choice = "Yes" ]; then

I'm also not sure what you would want this to do:

user_input routers[@] router_number R 

You may want

routers=( $(user_input "$router_number" R) )

or something similar (and modify your user_input function to match that).

You should also double quote the variable expansions. See "Security implications of forgetting to quote a variable in bash/POSIX shells" as to why.

Paste your script into ShellCheck to get a fuller list of possible problems: https://www.shellcheck.net/

As a general tip, you may want to test run your script as you develop it, just to make sure that what you've just written actually works. As opposed to writing a complete script before testing it at all.

Another tip to avoid lengthy if-then-elif bits is to use case ... esac:

case "$1" in
    Router)
        echo 'How many routers?'
        read router_number
        # etc.
        ;;
    Switch)
        # stuff for switches
        ;;
    Host)
        # stuff for hosts
        ;;
    *)
        echo 'Something is wrong' >&1
        exit 1
        ;;
esac

This also allows for easy pattern matching. R*) in a "case label" will match Router and any other string that starts with R. R*|H*) would match any string that starts with either R or H, etc.

2
  • 1
    @Kuslananda +1 Your explanations are exemplary. Commented Jun 2, 2017 at 19:57
  • for((i=1; i <= $2; ++i)); do would be OK even if not "canonical", ( can act as a token delimiter (like <, ; or | but contrary to [ or { for instance). So things like if(cmd) or while<foo cmd should work. Commented Jun 2, 2017 at 21:20

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.