UNIX head command not working?

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.

  1. Relevant commands, code, scripts, algorithms:

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

  1. The attempts at a solution (include all code and scripts):
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.

  1. 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).

What are you typing into unix?

myscript 1 2 3

or something similar, to start your script and pass parameters.

I type in

lshead -head 2 Documents (or whatever directory I want)

$0 = lshead command
$1 = -head/-tail
$2 = numeric value
$3 = directory

Isn't that error message highly meaningful? Look at the positional parameter that you supply to the commands.

---------- Post updated at 18:18 ---------- Previous update was at 18:17 ----------

And, why do you expect a negative parameter count?

I just put the $2 command there so that if a negative integer was entered, then it would prompt the user to enter a positive one.

And what about if the given directory were empty or contain binaries?
And to say the truth I doubt tail/head work on directories...

We haven't been told about what would happen if it was empty or contained binaries.

This is one of the commands I have been set so it must work surely.

Have you tried?

All the directories I try have files in them.

I'm not sure what to do for binaries as I am fairly new to unix.

What I meant was did you try

 tail -2 <directory> 

What do you get for an output?
RudiC post is also relevant:
A hint: where in the submitted code do you use $2?...

I get this

-bash: syntax error near unexpected token `newline'

I am new to unix as I said so I am guessing the arguments need to be in order
e.g.
$#
$1
$2
etc

Please scrutinize both the assignment and your code snippet:

  • a directory is supplied as a parameter, but the files in that dir are to be head ed/ tail ed
  • $# is the count of the parameters supplied to a script/function

Unix - Special Variables

Read carefully...

My question about $2 was quite clear you know
All command have man pages, they are to be used time by time...
The commands can have different behaviour depending of your OS (which we dont know...) specially when a command is for text/data file and you submit binary...

Right I understand what the special variables do.

I now get what you were talking about with $2, I only just realised I haven't actually used it.

However I still don't understand why it is not working?

It is required to be entered as this in the command line:

lshead head/tail 2 Documents

As we dont know what OS you are using we cant figure out like that what error message you would be getting... but you were asked to try on command line tail -2 <a directory name> to see the behaviour of that command with a directory, we are not going to to your work for you...
Once you have tested and told us the result can we help you go further...
Since if you have corrected now your script to use $2 (which we hope is a numeric value...), you are left with solving how to input files for the given directory...

I have tried the command and it came up with this:
-bash: syntax error near unexpected token `newline'

$2 is a numeric value

Also the OS is Windows.

Wasnt that your error?
...
So now you see that you were asked to enter a directory, but the assignment was clear:
All files in that directory are to passed to tail/head
But in your script we see nothing of the sort...

I honestly thought that the code I had written was right, just needed tweaking to get rid of the error.

I'm assuming you pass the files in the directory to tail or head by using the -n option to specify the amount of lines.

No.
Read man head , man tail , and the man page of your shell.
And the script you posted has several weak points, as I alluded before. Try to go through it line by line, maybe with the shell's -vx options set.