完成した製品は、再帰的に指定されたディレクトリ内のすべてのものを数えます。引数が入力されていない場合は現在のものです。今は、指定されたディレクトリ内の何かを数えるために取得しようとしています。私は何かを数える最後のステートメントを得るのが難しいです。 それはディレクトリに0個のファイルをエコーします。
誰も私に何かヒントを与えることができますか?私はまだ初心者ですので、私はそれを簡単に取る、ありがとう!
#!/bin/bash
#A shell script program that counts recursively how many directories/files exist in a given directory.
declare -i COUNT=0
declare -i COUNT2=0
#The script will treat COUNT as an integer that is 0 until modified.
if [ "$#" -eq "0" ]
then
for i in *
do
((COUNT++))
done
((COUNT--)) #This is done because there is always an overcount of 1.
echo "There are $COUNT files and/or directories in the current directory here."
fi
if [[ -d $1 ]]
then
for i in $1
do
((COUNT++))
done
((COUNT--)) #This is done because there is always an overcount of 1.
echo "There are $COUNT files and/or directories in $1."
fi
if [[ -d $2 ]]
then
for i in $2
do
((COUNT2++))
done
((COUNT2--)) #This is done because there is always an overcount of 1.
echo "There are $COUNT2 files and/or directories in $2."
fi
exit 0
回答:
回答№1は4まず、ワンライナーであなたが望むことができます:
find . | wc -l
find .
現在のディレクトリとすべてのサブディレクトリで検索することを意味します。他に引数がないので、単純にすべてをリストします。それから、私はパイプと wc
これは "単語数"を表します。ザ -l
オプションは "行数だけを出力する"という意味です。
さて、あなたのコードのために、ここにいくつかのヒントがあります。まず、コードを3回(0、$ 1、$ 2)繰り返す理由を本当に理解していません。
dir="$1"
if [ -z "$dir" ]; then dir="."; fi
コマンドライン引数の値を$ dirに格納します。指定されていない場合(-zは "空"を意味します)、dirにデフォルト値を割り当てます。
for i in $1
もしうまくいけば $1
ディレクトリへのパスです。代わりに、あなたは
for i in $(ls $dir)
また、あなたのコードでは、あなたは再帰的にカウントしません。それは自発的なのでしょうか、進めるか分からないのですか?