lsn:
# add by cw
# aim: print nth file contents
if [ $# -eq 1 ]; then
xnd=`echo $1'p'`
nfilename=`ls | sed -n $xnd`
echo 'filename:'\<$nfilename\>
if [ -d "$nfilename" ]; then
echo "is a directory!"
exit
fi
echo '------------- contents: --------------'
ls | sed -n $xnd | xargs cat
elif [ $# -eq 2 ]; then
xnd=`echo $2'p'`
nfilename=`ls $1 | sed -n $xnd`
echo 'filename:'\<$nfilename\>
echo '------------- contents: --------------'
file2=`ls $1 | sed -n $xnd`
abs_path=`echo $1\/$file2`
if [ -d "$abs_path" ]; then
echo "is a directory!"
exit
fi
cat $abs_path
echo ''
else
echo "lsn parameter error"
fi
本文介绍了一段Shell脚本,该脚本能够根据参数打印当前目录或指定目录下第n个文件的内容,同时检查是否为目录并进行错误处理。
86

被折叠的 条评论
为什么被折叠?



