home *** CD-ROM | disk | FTP | other *** search
/ ARM Club 3 / TheARMClub_PDCD3.iso / hensa / unix / unixcmds_1 / BIN / tail < prev    next >
Encoding:
Text File  |  1993-06-10  |  692 b   |  49 lines

  1. # Perl version of tail.
  2. # Usage : tail [-#] file1 file2 ...
  3. #
  4.  
  5. # Is the first argument a -# ?
  6. if (substr($ARGV[0], 0, 1) eq "-")
  7. {
  8.     # Yes, so read it.
  9.     $lines = -(shift);
  10. }
  11. else
  12. {
  13.     # No, so default to ten lines.
  14.     $lines = 10;
  15. }
  16.  
  17. # We are working modulo $lines, so increment it.
  18. $lines++;
  19.  
  20. # For each file specified.
  21. foreach (<@ARGV>)
  22. {
  23.     # Print the filename.
  24.     print "\n--- ".$_." ---\n\n";
  25.     
  26.     # Print the first few lines of the file.
  27.     open($input, $_);
  28.     $i = 0;
  29.     while ($line[($i++) % $lines] = <$input>)
  30.     {
  31.     }
  32.     
  33.     if ($i < $lines)
  34.     {
  35.         for ($j=0; $j<$i; ++$j)
  36.         {
  37.             print $line[$j];
  38.         }
  39.     }
  40.     else
  41.     {
  42.         for ($j=$i-$lines; $j<$i; ++$j)
  43.         {
  44.             print $line[$j % $lines];
  45.         }
  46.     }
  47.     close($input);
  48. }
  49.