![パラメータのないスクリプトはメッセージをエコーする必要がありますが、[閉じられません]](https://linux33.com/image/71481/%E3%83%91%E3%83%A9%E3%83%A1%E3%83%BC%E3%82%BF%E3%81%AE%E3%81%AA%E3%81%84%E3%82%B9%E3%82%AF%E3%83%AA%E3%83%97%E3%83%88%E3%81%AF%E3%83%A1%E3%83%83%E3%82%BB%E3%83%BC%E3%82%B8%E3%82%92%E3%82%A8%E3%82%B3%E3%83%BC%E3%81%99%E3%82%8B%E5%BF%85%E8%A6%81%E3%81%8C%E3%81%82%E3%82%8A%E3%81%BE%E3%81%99%E3%81%8C%E3%80%81%5B%E9%96%89%E3%81%98%E3%82%89%E3%82%8C%E3%81%BE%E3%81%9B%E3%82%93%5D.png)
パラメータが指定されていない場合でも、私のスクリプトが条件を実行しない理由は疑問に思います。私は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" ]
。