shell编程---十分火急 谢谢大家啦

来源:百度知道 编辑:UC知道 时间:2024/04/29 14:56:49
编写程序,接受一个参数,然后检查此函数是文件还是目录,如果是目录,程序显示该目录下的所有文件和子目录的数量,不包括隐含文件;如果是参数是一个文件,程序显示文件的名字和大小。

要求如下:
$ listmore xy ./xy目录有两个文件

显示 the number of files in the directory is 2

$ listmore file1

显示 Name Size
file1 1857

脚本如此

#! /bin/sh
# POSIX shell version
usage="Usage: `basename $0` [directory(ies)|file(s)]"

if [ $# -lt 1 ]
then
echo "$usage" 1>&2
exit 5
fi

for i in $@
do
if [ -d "$i" ]
then
howmany=`ls -l "$i" | sed '/^-/!d' | wc -l`
echo "the number of files in the $i is $howmany"
fi
if [ -f "$i" ]
then
size=`du -h "$i" | cut -f1`
echo "$i: $size"
fi

if [ ! -f "$i" ] && [ ! -d "$i" ]
then
echo "$i: not a directory nor a file"
fi
done
# end

这样差不多了,呵呵