bash - How to write a tail script without the tail command -


how achieve in bash. it's question got asked in interview , think of answers in high level languages not in shell.

as understand it, real implementation of tail seeks end of file , reads backwards.

the main idea keep fixed-size buffer , remember last lines. here's quick way tail using shell:

#!/bin/bash  size=5 idx=0  while read line     arr[$idx]=$line     idx=$(( ( idx + 1 ) % size ))  done < text  ((i=0; i<size; i++))     echo ${arr[$idx]}     idx=$(( ( idx + 1 ) % size ))  done 

Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -