質問: スクリプトは、任意の数のファイル名を引数として受け取ります。スクリプトは、指定されたすべての引数がファイルまたはディレクトリであるかどうかを確認する必要があります。ディレクトリ レポートの場合。ファイルの場合、ファイルの名前とその中に存在する行数を報告する必要があります。
以下は私のコードです、
#!/bin/sh
for i in $*; do
if [ -f $i ] ; then
c=`wc -l $i`
echo $i is a file and has $c line\(s\).
elif [ -d $i ] ; then
echo $i is a directory.
fi
done
出力:
shree@ubuntu:~/unixstuff/shells$ ./s317i file1 file2 s317h s317idir
file1 is a file and has 1 file1 line(s).
file2 is a file and has 2 file2 line(s).
s317h is a file and has 14 s317h line(s).
私の質問: 変数 c の値は、反復ごとに 1 file1、2 file2、14 s317h です。1、2、14にしたいのですが、なぜ後者ではなく前者の値が含まれているのですか? どこが間違っていますか?
注: s317i は私のファイル名であり、file1 file2 s317h および s317idir はコマンド ライン引数です。
親切にアドバイス。