×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: Bash
Posted by: Hariharan Radhakrishnan
Added: Nov 11, 2011 6:20 AM
Modified: Nov 11, 2011 6:20 AM
Views: 42
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