Quantcast
Channel: UNIX and Linux Forums
Viewing all articles
Browse latest Browse all 16232

UNIX head command not working?

$
0
0
Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted!

1. The problem statement, all variables and given/known data:

Write a script that allows the user to print the first n lines or the last n lines of every file in the directory specified by the argument. e.g. lshead -head 2 Documents will print the first two lines of every file in the documents directory.


2. Relevant commands, code, scripts, algorithms:

Head command is used in this command as well as the tail command.

3. The attempts at a solution (include all code and scripts):

Code:

if [ $# -lt 0 ];
then
    echo "A directory name as an argument is expected"
    exit 1
fi

if [ $2 -lt 0 ];
then
        echo "expected a positive integer"
        exit 1
fi

if [ $1 = "-head" ];
then
    head -n $3

elif [ $1 "-tail" ];
then
    tail -n $3
else
        echo "Enter either head or tail"
        exit 1
fi
exit 0

I have run it and it comes up with this error:
head: Documents: invalid number of lines

I don't understand why it isn't working, I have even consulted friends on this and even they can't understand why it isn't working.

4. Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):
University of Huddersfield, Huddersfield, United Kingdom, Dr Gary Allen, CFT2112

Note: Without school/professor/course information, you will be banned if you post here! You must complete the entire template (not just parts of it).

Viewing all articles
Browse latest Browse all 16232

Latest Images

Trending Articles



Latest Images