簡體   English   中英

Bash:在case語句中的if / else語句中,Else不起作用

[英]Bash:Else not working in if/else statement in case statement

我正在嘗試檢查用戶是否使用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

打印第一個條件,但是如果輸入例如del 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

盡管尚不清楚為什么需要在此處設置,但您不能使用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 ,而不僅僅是命令。

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

如果您真的在使用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 指出的那樣read是有問題的:它只是將輸入拆分為用空格分隔的單詞,而不考慮引號,反斜杠以及您可能要實現的任何其他shell元字符。 在bash本身中對命令行進行完整的bash樣式解析將是非常困難的練習。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM