使用KSH返回与大部分线路文件中的目录

问题描述 投票:0回答:2

我在那里脚本我期待的文件数最多的目录线返回一个KSH文件。该脚本只能接受一个参数,并且必须是一个有效的目录。我有2个错误情况想通了,但我有与最大线部分到目前为止,我有以下文件的麻烦:

#!/bin/ksh
#Script name: maxlines.sh
ERROR1="error: can only use 0 or 1 arguments.\nusage: maxlines.sh [directory]"
ERROR2="error: argument must be a directory.\nusage: maxlines.sh [directory]\n"
$1
if [[ $# -gt 1 ]]
        then
                printf "$ERROR1"
                exit 1
fi
if [ ! -d "$1" ]
        then
        prinf "$ERROR2"
fi
for "$1"
do
    [wc -l | sort -rn | head -2 | tail -1]

从我一直在寻找的最大线将来自使用厕所,但我不确定格式化的,因为我还是新的shell脚本。任何意见将帮助!

linux ksh wc
2个回答
1
投票
> for "$1"
> do
>    [wc -l | sort -rn | head -2 | tail -1]

for循环有一个小语法错误,并在方括号是完全错误的。你并不需要一个循环,无论如何,因为wc接受的文件名参数列表。

wc -l "$1"/* | sort -rn | head -n 1

最上面一行,而不是第二行,将包含的行数最多的文件。也许你想添加一个选项修剪掉的数量并只返回文件名。

如果您在以上的$1文件要循环,那会是什么样子

for variable in list of items
do
    : things with "$variable"
done

其中list of items可能是通配符表达式"$1"/*(如上}和do ... done拿,你可以想象你想要方括号的地方。

(方括号在比较中使用; [ 1 -gt 2 ]运行[命令来比较两个数字可以比较了很多不同的东西 - 字符串,文件等ksh还具有具有比传统[[一些功能的更发达的变种[。 。)


0
投票

我的报价是有点生疏,但试试这个Bourne shell脚本:

#!/bin/sh
#Script name: maxlines.sh
ERROR1="error: can only use 0 or 1 arguments.\nusage: maxlines.sh [directory]"
ERROR2="error: argument must be a directory.\nusage: maxlines.sh [directory]\n"
echo argument 1: "$1"
if [ $# -gt 1 ]
        then
        echo "$ERROR1"
    exit 1
fi
if [ ! -d "$1" ]
        then
        echo "$ERROR2"
    exit 1
fi
rm temp.txt
#echo "$1"/*
for i in "$1"/*
    do
    if [ -f "$i" ] 
        then
            #echo 2: $i
            wc -l "$i" >> temp.txt
        #else echo $1 is not a file!
    fi
    done
cat temp.txt | sort -rn | head -1
© www.soinside.com 2019 - 2024. All rights reserved.