パラメータのないスクリプトはメッセージをエコーする必要がありますが、[閉じられません]

パラメータのないスクリプトはメッセージをエコーする必要がありますが、[閉じられません]

パラメータが指定されていない場合でも、私のスクリプトが条件を実行しない理由は疑問に思います。私はifステートメントの順序かもしれませんが、わかりません。どんな提案がありますか?これは、スペースが誤って配置されているような単純なエラーではないようです。

for param in "$@";
do

    if [[ -n $confirm ]]; #this is a getopts switch asking for confirmation like rm -i
    then
        #asks user whether to confirm deletion
        if [ $answer != [Yy]* ]];
        then
            continue #go to next param argument
        fi
    fi

    if [ -z "$param" ] #if no argument has been specied,
    then
         #print an error that additional operand is needed.

    elif [ -d ./$param ] #if a directory name is specified
    then
        if [[ -n $recursive]] #getops recursive switch, like rm -r
        then
            #recursively delete a directory
        fi
        #error message about deleting directory without -r switch
    elif [ ! -e ./$param ] 
    then
        #If not an existing file either then print error that there is no such file or directory
    elif [[ $param = "safe_rm" ]] || [[ $param = "safe_rm_res" ]]
    then
        #This prevents script from trying to delete itself or restore script
    fi


    if [[ -n $verbose ]] third and final getopts switch, similar to rm -v
    then
        #message confirming deletion
    fi
done

私のコードはごみ箱の作成に関するもので、スクリプトがrm同じrm -i -v方法でスイッチを持って使用するコマンドに基づいています-r。上記のifステートメントは、パラメーターに従って削除の処理方法を変更します。最初のifステートメントは、パラメーターがディレクトリかどうかについてです。 2 番目はファイルなのか、3 番目は空なのか、4 番目はパラメータが自分自身なのか (自己削除) です。

ベストアンサー1

forパラメータを繰り返すために使用されるループは、条件(リストの終わり)が満たされると終了し、その文に移動しますdone。スクリプトがforパラメータなしでループに達すると、リストの開始は終了と同じで、ループの条件はfalseです。

元の例では、ループ内のコマンドは、空の文字列が指定された場合に誤った結果を生成します。変数「param」が空の場合、最初のケースは[ -d ./$param ]以前の現在のディレクトリと./一致し、スクリプトは空の文字列チェックに達します[ -z "$param" ]

おすすめ記事