Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted!
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.
Relevant commands, code, scripts, algorithms:
Head command is used in this command as well as the tail command.
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.
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).
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...
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...
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...
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.