Groups
Groups
Sign in
Groups
Groups
Linuxer家园
Conversations
About
Send feedback
Help
一个shell脚本程序中的疑点
0 views
Skip to first unread message
刘周平
unread,
Jul 24, 2008, 6:28:38 AM
7/24/08
Reply to author
Sign in to reply to author
Forward
Sign in to forward
Delete
You do not have permission to delete messages in this group
Copy link
Report message
Show original message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to linuxe...@googlegroups.com
下面的shell脚本程序主要想达到这个目的:用一个目录名作为参数,将该目录下的所用文件中名字最长的那个文件名写到标准输出上,如果函数的参数不是一个目录,那么就向标准输出输出一个错误信息,同时返回一个非零退出码退出程序。下面的程序基本上实现了目的,但是出现了好多好多的错误提示,调试了好多边,也没去除掉错误提示,不知道是怎么回事,请高手指点一下。
1 liuzhouping()
2 {
3 i=0
4 if [ -d $1 ] && [ $# = 1 ];then
5 j=${1}/
6 echo $j
7 set $(ls $1)
8 echo "$j/$*"
9 for lzhp in $*
10 do
11 if [ -f $($j$lzhp) ];then
12 if [ $i -lt ${#lzhp} ];then
13 j=$lzhp
14 i=${#lzhp}
15 fi
16 fi
17 done
18 echo $j
19 echo "该目录下文件最大长度为:${#j}"
20 else
21 echo "你输入的不是目录,或者参数不是只有一个!" 1>&2
22 exit 1
23 fi
24 }
25 liuzhouping $@
问题就在第11行,这样写第11行主要是相让文件名带上
自己的根目录
,(不然该程序只能在当前环境下运行)可是不知道怎么去给文件名
带上自己的根目录呢?
刘周平
unread,
Jul 25, 2008, 5:18:10 AM
7/25/08
Reply to author
Sign in to reply to author
Forward
Sign in to forward
Delete
You do not have permission to delete messages in this group
Copy link
Report message
Show original message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to linuxe...@googlegroups.com
2008/7/24 刘周平 <
sanwei...@gmail.com
>:
之后,我又调试了一下,找到错误了,就是for语句中的变量没控制好,下边是我改正过的正确程序。(这个程序还不完善,没考虑周全,大家也可以继续补充)
liuzhouping()
{
i=0
if [[ -d $1 && $# = 1 ]];then
k=$1
set $(ls $1)
for lzhp in $*
do
if [ -f $k/$lzhp ];then #$k记录的是该文件的目录
if [ $i -lt ${#lzhp} ];then
j=$lzhp
i=${#lzhp}
fi
fi
done
echo $j
echo "该目录下文件最大长度为:${#j}"
else
echo "你输入的不是目录,或者参数不是只有一个!" 1>&2
exit 1
fi
}
liuzhouping $@
Reply all
Reply to author
Forward
0 new messages