Question: Simple Shell Scripting For Linux - Please replace the XXXs in the script below to solve this: Write a script that will determine which file

Simple Shell Scripting For Linux - Please replace the XXXs in the script below to solve this:

Write a script 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 should onlyfocus on files and ignore sub directories. Research the wc command options under the manual online documentation.

The maxlines.sh script is designed to handle zero (0) or one (1) argument(s).

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

If one argument is specified, the argument must be a valid directory name. The script will then examine the files in the specified directory.

The script should 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 must be located in $HOME/itec400/homework

Make sure the permissions on the your itec400 directory are 705

Make sure the permissions on your script are 705

Maxlines.sh script logic:

#!/bin/ksh

# Script name: maxlines.sh

# Case #1: ./maxlines.sh

# Case #2: ./maxlines.sh /bin

# Case #3: ./maxlines.sh .

# Functionality: This script reads each file in a directory and counts the

# number of new lines. After determining this count, the file name and new

# line count are saved. The new line count is then compared to each

# file to determine the file with the highest new line count.

# Output an error message because more than one argument is present on the

# command line.

ERROR1="error: can only use 0 or 1 arguments. usage: maxlines.sh [directory]"

ERROR2="error: argument must be a directory. usage: maxlines.sh [directory] "

# Verify that more than one command line argument doesn't exist.

XX [[ XX -gt 1 ]]

then

printf "$ERROR1"

exit 1

fi

# Default to the current directory Case 1 and 3

DIR="."

# Case #2 and 3.

XX[[ $# -eq X ]]

then

# Is the directory entry a directory?

if [[ XX $1 ]]

then

# Assign Variable DIR to command line directory.

DIR="$1"

else

printf "$ERROR2"

exit 1

fi

fi

# All Cases are processed with the following code.

cd $DIR

FILE=$(wc -l `ls` 2>/dev/null | sort XXX | tail XXX | head XXX)

printf "File `echo $FILE | awk '{print $2}'` has the maximum lines with `echo $FILE | awk '{print $1}'` lines. "

exit X

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!