if文を使用して出力メッセージを変更する方法

if文を使用して出力メッセージを変更する方法

このコマンドを実行すると、ごみ箱ディレクトリに何もない場合でも同じメッセージが出力されます。ごみ箱にファイルがない場合にコマンドに別のメッセージを出力させるにはどうすればよいですか?

            #! /bin/bash
            #! listwaste - Lists the names of all the files in your waste bin and their size
            #! Daniel Foster 23-11-2015

            echo "The files that are in the waste bin are:"

            ls -1 ~/bin/.waste/

私はこれが簡単でなければならないことを知っていますが、ちょうど始まったばかりなので、ifステートメントまたはそれに似たものを使用する必要があると思います。

あなたの助けに事前に感謝の言葉を伝えたいと思います。

ベストアンサー1

変数に出力を割り当てることは、次のように異なる動作をします。

$ mkdir ~/bin/.waste
$ OUTPUT=$( ls -1 ~/bin/.waste )
$ if [[ -z "$OUTPUT" ]]; then echo no waste; else echo $OUTPUT; fi
no waste
$ touch ~/bin/.waste/blkasdjf
$ OUTPUT=$( ls -1 ~/bin/.waste )
$ if [[ -z "$OUTPUT" ]]; then echo no waste; else echo $OUTPUT; fi
blkasdjf
$ 

おすすめ記事