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

by Vivek Gite [Last updated: September 20, 2008]

in File-management, String Management

#!/bin/bash
# Write a shell script like a more command. It asks the user name, the
# name of the file on command prompt and displays only the 15 lines of
# the file at a time.
# -------------------------------------------------------------------------
# Copyright (c) 2007 nixCraft project <http://cyberciti.biz/fb/>
# This script is licensed under GNU GPL version 2.0 or above
# -------------------------------------------------------------------------
# This script is part of nixCraft shell script collection (NSSC)
# Visit http://bash.cyberciti.biz/ for more information.
# -------------------------------------------------------------------------
 
counter=1
echo -n "Enter a file name : "
read file
 
if  [ ! -f $file ]
then
	echo "$file not a file!"
	exit 1
fi
 
# read file line by line
exec 3<&0
while read line
do
       # pause at line no. 15
	if [ $counter -eq 15 ]
	then
		counter=0 # reset counter
		echo " *** Press [Enter] key to continue ..."
		read -u 3 enterKey
	fi
	echo $line
	(( counter++ ))
done < $file
Want to read Linux tips and tricks, but don't have time to check our blog everyday? Subscribe to our email newsletter to make sure you don't miss a single tip/tricks.

{ 1 comment… read it below or add one }

ghostdog74 09.20.08 at 1:01 pm

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

Leave a Comment

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Tagged as: , , , , , , , , ,

Previous post: Shell Script To Count Number Of Files In Each Subdirectories

Next post: Shell Script To Read File Date - Last Access / Modification Time