Bash脚本功能

时间:2016-10-21 10:10:10

标签: bash shell

为大学作业编写自定义Shell脚本。几乎所有事情都与此分开...... 我有一个名为ifc的别名,它将调用一个名为functionIfc的函数。但是,当使用此代码时,它会告诉我意外的令牌'}'}找到了。似乎无法调试这个。被困在上面一段时间了。任何帮助将不胜感激!

  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
}

1 个答案:

答案 0 :(得分:0)

你应该用if关闭每个if语句,如下所示;

-

您可以在下面定义别名;

var strngs = ['stringVar=email:name@company.com&room:b83edd0d-fec5-4ec0-8055-60bc0d8eb4b6;', 'stringVar=email:name@company.com&room:b83edd0d-fec5-4ec0-8055-60bc0d8eb4b6&host=HOSTNAME;', 'stringVar=email:name@company.com&location=SomeCity&room:b83edd0d-fec5-4ec0-8055-60bc0d8eb4b6;'];
var re = /&room:([a-f\d-]+)/i;
for (var s of strngs) {
  var res = (m=s.match(re)) ? m[1] : "";
  console.log(s + "\n=>\n" + res); // demo
}

你可以在同一个终端使用这个别名。

例如

#!/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