Skip to content
Advertisement

KSH shell that counts lines of files in directory

This is the prompt:

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.

  1. If zero arguments are specified, the script by default will examine the files in the current directory.
  2. 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:

  1. More than 1 argument is specified.
  2. The specified argument is not a directory.

The script file name must be: maxlines.sh. The script permissions should be 705.

This is the code I have:

#!/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 < "$file" ) ))
    if [ $max -lt $lines ];then
        max=$lines
    fi
done

Any advice would be appreciated. I am new to Linux so this is quite frustrating. When I run it the output looks like this:

./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

Advertisement

Answer

As @JeremyBrooks already explained, there are a lot of errors in your script, but you’re not on completely the wrong path.

This is my fix:

#!/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 < "$file" ) ))
            if [ $max -lt $lines ];then
                    max=$lines
                    maxFile=$file
            fi
    done
    echo "file[$maxFile] - max[$max]"

With for file in $(find . -name "*" -type f) you’ll get only files; in your previous version you’ll get also the directories, but you want to count the file lines.

Another way to prevent exclude directories is for file in $(ls -l * | grep -v "^d"|awk '{print $9}', but that is not fast as find.

You also forgot to keep the file name when you get the max line number, so in the if you have to add maxFile=$file.

Then don’t forget to print out the result: echo "file[$maxFile] - max[$max]" after the for.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement