shell指令碼執行個體 1、切換工作目錄至位置參數給出的目錄,依次向這些目錄中的每個檔案或子目錄問好,統計這些目錄下的各種檔案及子目錄個數,並顯示。 01#! /bin/sh02 03#切換工作目錄至位置參數給出的目錄04#依次向這些目錄中的每個檔案或子目錄問好05#統計這些目錄下的各種檔案及子目錄個數,並顯示06 07if [ $# -lt 1 ]08then09 echo "parameter's number error!";10 exit 1;11fi;12 13while [ $# -gt 0 ]14do15 if [ -f $1 ]16 then17 echo "$1 is not a dir!";18 else 19 cd $120 21 bfiles=0; #塊裝置檔案22 cfiles=0; #字元裝置檔案23 pfiles=0; #管道檔案24 lfiles=0; #連結檔案/目錄25 sfiles=0; #sock檔案26 files=0; #普通檔案27 dirs=0; #目錄檔案28 total=0; #總檔案數29 for var in `ls $1`30 do31 total=`expr $total + 1`;32 echo "hello $var";33 if [ -b $var ]34 then35 bfiles=`expr $bfiles + 1`; 36 elif [ -c $var ]37 then38 cfiles=`expr $cfiles + 1`;39 elif [ -S $var ]40 then41 sfiles=`expr $sfiles + 1`;42 elif [ -p $var ]43 then44 pfiles=`expr $pfiles + 1`;45 elif [ -h $var ]46 then47 lfiles=`expr $lfiles + 1`;48 elif [ -f $var ]49 then50 files=`expr $files + 1`;51 elif [ -d $var ]52 then53 dirs=`expr $dirs + 1`;54 fi;55 done56 57 echo "the number of bfiles is $bfiles";58 echo "the number of cfiles is $cfiles";59 echo "the number of lfiles is $lfiles";60 echo "the number of sfiles is $sfiles";61 echo "the number of pfiles is $pfiles";62 echo "the number of files is $files";63 echo "the number of dirs is $dirs";64 echo "total file number is $total";65 fi;66 echo "************************************"67 shift;68done