2017-02-16 159 views
0

我想檢查用戶是否在命令行中使用case和if/else語句鍵入多個參數。有什麼不對的是,我一直得到默認情況下,而不是相同的命令,但有2多個參數。例如,我的條件之一是:Bash:否則在case語句中的if/else語句不工作

del) 
    if [ -z "$2" ] || [ -z "$3" ] 
    then 
      echo "Usage: removes a file" 
    else 
    echo "using Bash command: rm $2 $3" 
    rm $2 $3 
    echo done 
    fi 

打印的首要條件,但如果我輸入,也就是說,德爾AAA BBB,我得到默認情況下,它是:

echo "ERROR: Unrecognized command" 

我如果有幫助,也可以使用它讀取用戶的輸入。

read -p "wcl> " -r wcl $2 $3 

我真的不知道是否有解決這個不殺我所有的代碼,並從頭開始一個更好的辦法。 這是全碼:

#!/bin/bash 
#use read command 

echo Welcome to the Windows Command Line simulator! 
echo Enter your commands below 
while true 
do 
read -p "wcl> " -r wcl $2 $3 
    case $wcl in 
    dir) 
    echo "using Bash command: ls $2 $3" 
    ls 
    continue 
      ;; 
    copy) 
    FILE="$2" 
    if [ "$#" -ne 3 ] 
    then 
     echo "Usage: copy sourcefile destinationfile" 
    else 
    echo "using Bash command: cp $2 $3" 
    if [ -f "$FILE" ] 
    then 
    cp $2 $3 
    else 
    echo "cannot stat $FILE: No such file or directory">&2 
    fi 
    echo done 
    fi 
    continue 
      ;; 
    del) 
    if [ -z "$2" ] || [ -z "$3" ] 
    then 
      echo "Usage: removes a file" 
    else 
    echo "using Bash command: rm $2 $3" 
    rm $2 $3 
    echo done 
    fi 
    continue 
      ;; 
    move) 
    if [ -z "$2" ] || [ -z "$3" ] 
    then 
      echo "Usage: moves a file to another file name and location" 
    else 
    echo "using Bash command: mv $2 $3" 
    mv $2 $3 
    echo done 
    fi 
    continue 
      ;; 
    rename) 
    if [ -z "$2" ] || [ -z "$3" ] 
    then 
      echo "Usage: renames a file" 
    else 
    echo "using Bash command: mv $2 $3" 
    mv $2 $3 
    echo done 
    fi 
    continue 
      ;; 
    ipconfig) 
      ifconfig eth0 | grep "inet addr" | cut -d ':' -f 2 | cut -d ' ' -f 1 
    continue 
      ;; 
     exit) 
      echo "Goodbye" 
      exit 1 
      ;; 
    ^c) 
      echo "Goodbye" 
      exit 1 
      ;; 
*) 
      echo "ERROR: Unrecognized command" 
    continue 
esac 
done 
+0

檢查命令行參數個數的最好方法是測試'$#',例如'if(($#<2))'。 – cdarke

+0

你需要顯示你的代碼的其餘部分,特別是'case .. in'部分。 – SiKing

回答

0

您不能使用read設置位置參數,雖然目前尚不清楚爲什麼你會需要在這裏。只需使用常規參數。

while true 
do 
    read -p "wcl> " -r wcl arg1 arg2 
    case $wcl in 
    dir) 
    echo "using Bash command: ls $arg1 $arg2" 
    ls "$arg1" "$arg2" 
    continue 
      ;; 

    # ... 
    esac 
done 

執行方式read -r wcl $2 $3$2$3首先膨脹,得到的是read將用來設置變量的名稱。如果沒有設置,那麼該命令將減少到read -r wcl,因此整個命令行分配給變量wcl,而不僅僅是命令。

但是,read本身不會執行與shell已經執行的相同的分析,如果您的目標是編寫自己的shell。

+0

我試過,現在它只是打印第一個條件,而不是其他條件,即「回聲」用法:...「而不是」使用...「 – anon

+0

這是因爲'$#'不受'read 「所有的事情都需要你重新思考。 – chepner

0

如果你真的在使用bash,你可以通過數組插入你讀到的位置參數。 (你也可以只將它們留在陣中,但指的是位置參數的語法比較簡單。)

# -a: read the successive words into an array 
read -r -p "wcl> " -a params 
# set the positional parameters to the expansion of the array 
set -- "${params[@]}" 
wcl=$1 # Or you could do the case on "$1" 

這也將設置$#單詞讀的數量,設置的副作用位置參數。

由於@chepner points outsread是有問題的:它只是將輸入到空格分開的話,不尊重引號,反斜槓,以及其它shell元字符,你可能要實現。在bash中對命令行進行完整的bash風格解析本身就是一項相當困難的工作。