Shell Script To Simulate UNIX More Command To Display Only The 15 Lines At A Time

by on September 20, 2008 · 3 comments

  1. #!/bin/bash
  2. # Write a shell script like a more command. It asks the user name, the
  3. # name of the file on command prompt and displays only the 15 lines of
  4. # the file at a time.
  5. # -------------------------------------------------------------------------
  6. # Copyright (c) 2007 nixCraft project <http://cyberciti.biz/fb/>
  7. # This script is licensed under GNU GPL version 2.0 or above
  8. # -------------------------------------------------------------------------
  9. # This script is part of nixCraft shell script collection (NSSC)
  10. # Visit http://bash.cyberciti.biz/ for more information.
  11. # -------------------------------------------------------------------------
  12.  
  13. counter=1
  14. echo -n "Enter a file name : "
  15. read file
  16.  
  17. if [ ! -f $file ]
  18. then
  19. echo "$file not a file!"
  20. exit 1
  21. fi
  22.  
  23. # read file line by line
  24. exec 3<&0
  25. while read line
  26. do
  27. # pause at line no. 15
  28. if [ $counter -eq 15 ]
  29. then
  30. counter=0 # reset counter
  31. echo " *** Press [Enter] key to continue ..."
  32. read -u 3 enterKey
  33. fi
  34. echo $line
  35. (( counter++ ))
  36. done < $file


4000+ howtos and counting! If you enjoyed this article, join 45000+ others and get free email updates!

Click here to subscribe via email.

  • ghostdog74

    you can just do [code]more -15 file[/code]

  • shadyabhi

    @ghostdog74
    The question is about doing it without using the more command…

  • supriya Kulkarni

    please send the c code for the above shell script

Previous Script:

Next Script: