2016-10-21 9 views
-1

大学の割り当てのためのカスタムシェルスクリプトの作成。実際にはこれとは別にすべての作業があります。 functionIfcという関数を呼び出すifcというエイリアスがあります。ただし、このコードを使用すると、予期しないトークン '}'が見つかったことがわかります。これをデバッグできないようです。しばらくそれに固執していた。どんな助けでも大歓迎です!Bashスクリプト機能

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 
} 
+0

バッシュ関数は()で宣言されていますか?おそらくスペースとafter()の間を使用してください... – Ko2r

+0

http://www.shellcheck.net/のコードを文法チェックのために貼り付けることを検討してください。 [Here](http://stackoverflow.com/a/6212408/1983854)関数を定義する2つの方法を見ることができます。@ Ko2r – fedorqui

答えて

0

if文を次のようにfiで閉じる必要があります。

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

あなたは以下のように別名を定義することができます。

alias ifc="/yourscriptpath/" 

このエイリアスは同じ端末で使用できます。

例。

[email protected] $ alias ifc="/tmp/test/t/test.sh" 
[email protected] $ 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> ... 
+1

これがなぜこの問題を解決しているのかについてコメントを追加するのはどうですか? – fedorqui