Micheal_S Micheal_S - 1 month ago 10
Bash Question

Bash Script Function

Writing a custom Shell Script for a college assignment. Have practically everything working apart from this....
I have an alias called ifc which will call a function called functionIfc. However, when this code is used, it tells me that unexpected token '}' is found. Cannot seem to be able to debug this. Been stuck on it for a while. Any help would be greatly appreciated!

alias ifc="functionIfc";

functionIfc(){

echo "Please enter which ethernet you would like to display"
echo -e "\n Command ifc1: Shows loopback"
echo -e "\n Command ifc2: Shows eth 0"
echo -e "\n Command ifc3 Shows eth 1"
echo -e "\n Command ifc4 shows eth 2"
echo -e "\n Command ifc5 shows eth 3"

read INPUT

if [ $(INPUT) == "ifc1" ]; then
echo $(ip addr show lo)

else if [ $(INPUT) == "ifc2" ]; then
echo $(ip addr show eth0)


else if [ $(INPUT) == "ifc3" ]; then
echo $(ip addr show eth1)

else if [ $(INPUT) == "ifc4" ]; then
echo $(ip addr show eth2)

else if [ $(INPUT) == "ifc5" ]; then
echo $(ip addr show eth3)

else
echo Not a valid entry, Try again

fi
}

Answer

you should close each if statement with fi as below;

#!/bin/bash 
  functionIfc(){
         echo  "Please enter which ethernet you would like to display"
         echo -e "\n Command ifc1: Shows loopback"
         echo -e "\n Command ifc2: Shows eth 0"
         echo -e "\n Command ifc3  Shows eth 1"
         echo -e "\n Command ifc4 shows eth 2"
         echo -e "\n Command ifc5 shows eth 3"

    read INPUT


    if [ ${INPUT} == "ifc1" ]; then
            echo $(ip addr show lo)

            else if [ ${INPUT} == "ifc2" ]; then
                   echo $(ip addr show eth0)


                    else if [ ${INPUT} == "ifc3" ]; then
                          echo  $(ip addr show eth1)

                            else  if [ ${INPUT} == "ifc4" ]; then
                                 echo $(ip addr show eth2)

                                    else if [ ${INPUT} == "ifc5" ]; then
                                            echo $(ip addr show eth3)

                                            else
                                                    echo Not a valid entry, Try again
                    fi
                fi
            fi
        fi

    fi
}

functionIfc

and

you can define an alias as below;

alias ifc="/yourscriptpath/"

and you can use this alias in same terminal.

Eg;

user@host $ alias ifc="/tmp/test/t/test.sh"
user@host $ ifc
Please enter which ethernet you would like to display

 Command ifc1: Shows loopback

 Command ifc2: Shows eth 0

 Command ifc3  Shows eth 1

 Command ifc4 shows eth 2

 Command ifc5 shows eth 3
ifc2
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> ...
Comments