Imitate head and tail command script

Hi,

I have been given assignment of 30 scripts out of which I was able to solve many, I need help with few out of which one asks to imitate head and tail command of unix without using the head and tail commands. Problem is stated below:

Write an interactive shell script to imitate the head and tail commands in unix.Dont use head and tail command and provide appropriate switch.

Can anyone please help me with that?

Sounds like a homework thing?

Anyway.

Interactive = read
Cat
wc for finding out how many lines there are
A handful of other commands
Awk for everything else

FILE=$1

echo "How many lines?: "
read HowMany

echo "Heads or tails? "
read HoT
case "$HoT" in
  h) awk 'NR <= '$HowMany $FILE ;;
  t) awk 'NR > '$(($(wc -l < $FILE) - $HowMany)) $FILE
esac

(there's a distinct lack of error checking here! $1 is the filename when calling this as a script from the command line)

Thanks it helped, I just needed to replace "file" with $FILE

yes, its an assignment more like a exam i will be having and I need to pass it :slight_smile: I dont intend to code for my job :slight_smile:

Thanks again.

Yes, I saw it.

Good luck!

since your question is "imitate head/tail without using head/tail" and nothing else, then you can use other language to do that. awk, Python, Perl..etc.

I have got it working with scott's help :slight_smile:

Thanks for replying.