×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: SQL
Posted by: Hariharan Radhakrishnan
Added: Nov 11, 2011 6:19 AM
Views: 37
GIVEN THE FILENAME BY THE USER AS THE INPUT, WRITE A SHELL SCRIPT TO DISPLAY THE LAST FIVE LINES OF THE FILE
  1. #!/bin/bash
  2. # GET filename
  3. echo -n "Enter File Name : "
  4. READ fileName
  5.  
  6. # make sure file exits FOR reading
  7. IF [ ! -f $fileName ]; THEN
  8.   echo "Filename $fileName does not exists"
  9.   exit 1
  10. fi
  11.  
  12. # display LAST five LINES OF the file USING tail command
  13. tail -5 $fileName
  14. COMMAND LINE ARGUMENT
  15. You can READ file name AS command line argument:
  16.  
  17. #!/bin/bash
  18. # GET filename
  19. fileName="$1"
  20. # make sure command line arg provided
  21. IF [ -z $1 ]; THEN
  22.         echo "Syntax: $(basename $0) filename"
  23.         exit 1
  24. fi
  25. # make sure file exits FOR reading
  26. IF [ ! -f $fileName ]; THEN
  27.   echo "Filename $fileName does not exists"
  28.   exit 1
  29. fi
  30. # ok display LAST five LINES OF the file USING tail command
  31. tail -5 $fileName