linux执行ksh文件,关于linux:KSH shell,它对目录中的文件行进行计数

这是提示:

Write a Korn shell script (and show the code for it here) that will determine which file in a directory has the maximum number of lines (this may be different than the file that has the maximum number of bytes). After determining the file with the maximum number of lines, the script will print out the name of the file and the number of lines. The script must only focus on files and ignore subdirectories. The command wc may be helpful. Do not write this script to be executed interactively.

Requirements:

The script must allow either no arguments or 1 argument.

If zero arguments are specified, the script by default will examine the files in the current directory.

If 1 argument is specified, the argument must be the name of a

directory. The script will then examine the files in the specified

directory.

The script must be able to handle the following error conditions:

More than 1 argument is specified.

The specified argument is not a directory.

The script file name must be: maxlines.sh.

The script permissions should be 705.

这是我的代码:

#!/bin/ksh

if [ $# -gt 1];then

echo"There must be 0 or 1 arguments"

exit

fi

if [ -d"$1" ];then

cd $#

fi

max=0

for file in *

do

lines=$(( $( wc -l

if [ $max -lt $lines ];then

max=$lines

fi

done

任何意见,将不胜感激。 我是Linux的新手,所以这很令人沮丧。 当我运行它时,输出如下所示:

./maxlines.sh[2]: [: ']' missing

./maxlines.sh[9]: cd: 1: [No such file or directory]

wc: standard input: Is a directory

wc: standard input: Is a directory

wc: standard input: Is a directory

wc: standard input: Is a directory

wc: standard input: Is a directory

wc: standard input: Is a directory

wc: standard input: Is a directory

wc: standard input: Is a directory

wc: standard input: Is a directory

wc: standard input: Is a directory

wc: standard input: Is a directory

wc: standard input: Is a directory

您有基本的语法错误。建议您首先阅读Shell编程教程-因为很明显您没有这样做。然后一次编写几行代码,而不是尝试一次完成全部操作。例如,使您的第一个if块正常工作。至少可以提高您对基本Shell脚本语法的了解(提示:if条件必须在then之前以;结尾,并且在]之前需要空格)。

我已经添加了";"在此过程的早期,但它总是给我带来错误。确实是因为我忽略了您所说的空间。感谢您指出这一点。问题不只是语法错误。我已经修复了它们,现在的输出是相同的,只是没有语法错误标志。谢谢您的帮助!

错误消息本身是否给您一个很明显的思考的思路?您的脚本尝试wc目标目录中的所有内容。该错误消息告诉您某些(子)目录正在传递给wc。计算目录中的行数显然没有任何意义。这就是wc发出该消息的原因。

另外,似乎您知道$#是什??么,因为它用于验证脚本是否被正确调用。因此,考虑到这一点,您期望cd $#做什么?也许您的意思是cd $1。似乎是一个基本的印刷错误。然后读取错误消息并进行代码检查。

您也可以尝试将命令与管道结合使用:

wc -l * 2>/dev/null| grep -v total | sort -n | tail -1 | sed -r 's/\s*([0-9]*) (.*)/\2 has \1 lines./'

您仍然应该处理参数(关于所拥有的参数,但在[和]的if语句中使用空格)。 以及如何处理没有文件的目录:

output=$(same_command_as_above)

if [ -z"${output}" ]; then ...

正如@JeremyBrooks已经解释的那样,您的脚本中存在很多错误,但是您并没有完全走错路。

这是我的解决方法:

#!/bin/ksh

if [ $# -gt 1 ];then

echo"There must be 0 or 1 arguments"

exit

fi

if [ -d"$1" ]

then

cd $1

fi

max=0

for file in $(find . -name"*" -type f)

do

lines=$(( $( wc -l

if [ $max -lt $lines ];then

max=$lines

maxFile=$file

fi

done

echo"file[$maxFile] - max[$max]"

使用for file in $(find . -name"*" -type f),您将只获得文件; 在以前的版本中,您还将获得目录,但您要计算文件行数。

防止排除目录的另一种方法是for file in $(ls -l * | grep -v"^d"|awk '{print $9}',但是速度不如find。

当获得最大行数时,您也忘记保留文件名,因此在if中必须添加maxFile=$file。

然后不要忘记打印结果:for之后的echo"file[$maxFile] - max[$max]"。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值