KSH shell 计算目录中文件的行数
KSH shell that counts lines of files in directory
这是提示:
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 "" ];then
cd $#
fi
max=0
for file in *
do
lines=$(( $( wc -l < "$file" ) ))
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
正如@JeremyBrooks 已经解释过的,你的脚本中有很多错误,但你并没有走完全错误的道路。
这是我的修复方法:
#!/bin/ksh
if [ $# -gt 1 ];then
echo "There must be 0 or 1 arguments"
exit
fi
if [ -d "" ]
then
cd
fi
max=0
for file in $(find . -name "*" -type f)
do
lines=$(( $( wc -l < "$file" ) ))
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 }'
,但速度不如 find
。
当你得到最大行号时你也忘记了保留文件名,所以在if
你必须添加maxFile=$file
。
然后不要忘记在for
.
之后打印出结果:echo "file[$maxFile] - max[$max]"
您也可以尝试将命令与管道结合使用:
wc -l * 2>/dev/null| grep -v total | sort -n | tail -1 | sed -r 's/\s*([0-9]*) (.*)/ has lines./'
您仍然应该处理参数(关于您拥有的参数,但在 [
和 ]
周围的 if 语句中使用空格)。以及如何处理没有文件的目录:
output=$(same_command_as_above)
if [ -z "${output}" ]; then ...
这是提示:
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 be705
.
这是我的代码:
#!/bin/ksh
if [ $# -gt 1];then
echo "There must be 0 or 1 arguments"
exit
fi
if [ -d "" ];then
cd $#
fi
max=0
for file in *
do
lines=$(( $( wc -l < "$file" ) ))
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
正如@JeremyBrooks 已经解释过的,你的脚本中有很多错误,但你并没有走完全错误的道路。
这是我的修复方法:
#!/bin/ksh
if [ $# -gt 1 ];then
echo "There must be 0 or 1 arguments"
exit
fi
if [ -d "" ]
then
cd
fi
max=0
for file in $(find . -name "*" -type f)
do
lines=$(( $( wc -l < "$file" ) ))
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 }'
,但速度不如 find
。
当你得到最大行号时你也忘记了保留文件名,所以在if
你必须添加maxFile=$file
。
然后不要忘记在for
.
echo "file[$maxFile] - max[$max]"
您也可以尝试将命令与管道结合使用:
wc -l * 2>/dev/null| grep -v total | sort -n | tail -1 | sed -r 's/\s*([0-9]*) (.*)/ has lines./'
您仍然应该处理参数(关于您拥有的参数,但在 [
和 ]
周围的 if 语句中使用空格)。以及如何处理没有文件的目录:
output=$(same_command_as_above)
if [ -z "${output}" ]; then ...