Read File or System Process and Continually Scroll Output Lines with Linux Command Line Script: readr

Read File or System Process and Continually Scroll Output Lines with Linux Command Line Script: readr
By Shay Anderson on November 2013
I recently created a Linux command line script (SH) readr that can read any file in the file-system and continually scroll the file contents. The script can also scroll through Linux command outputs. This type of script can be useful when monitoring files and/or system processes and services.

Usage
The readr usage is: Usage: /bin/readr “/path/to/read/file” 0.4(scroll speed, optional) true(flag as command and not file, optional)
Examples
Here is an example of continually scrolling through a log file using readr: # readr /var/log/syslogThis will continually scroll through the /var/log/syslog file. If any new lines are added to the log file they will be displayed as the script continually reads/scrolls the file.

You can also set a custom speed value for how fast the script scrolls through the file/process (the default speed value is 0.4, here is an example: # readr /var/log/syslog 0.1This example will speed up the scrolling.

You can use readr to continually read and scroll from an output from a system process or service, for example, say you are monitoring system process using the command: # ps aw -o pid,ppid,user,%cpu,%mem,rss,commandWith readr you can continually scroll through (and update) the processes being displayed, here is an example: # readr “ps aw -o pid,ppid,user,%cpu,%mem,rss,command” 0.4 trueThis will continually scroll through the system processes and display them one line at a time at the desired speed. The true flag in the command is used to tell readr we are reading from a system command and not a system file.

Script Source
Here is the readr source code: #!/bin/sh
#
# readr – file and command reader
#
# @author Shay Anderson 11.13

WAIT=”$2″;

clear

if [ -z "$1" ]
then
echo -n “readr – file and command reader\nCopyright (c) 2013 Shay Anderson \n\nUsage: $0 \”/path/to/read/file\” ”
echo “0.4(scroll speed, optional) true(flag as command and not file, optional)\n”;
exit;
fi

if [ -z "$WAIT" ]
then
WAIT=0.4
fi

while : ; do
if [ -n "$3" ]
then
$1|while read line; do
echo “$line”
sleep $WAIT;
done;
else
cat “$1″|while read line; do
echo “$line”;
sleep $WAIT;
done;

fi

echo “\n\n\n————————————————–\n”;
sleep 4;
done

Installation
You can simply copy the readr source to the system file /bin/readr and make the file executable: # cp -v readr /bin/readr
# chmod +x /bin/readr This will allow you to use the script like: # readr
Note: if you cannot or do not want to install the script on your system you can use a one line command to mimic the readr script, for example to read a file: # while : ; do cat /var/log/syslog|while read line; do echo “$line”; sleep 0.4; done; echo -e “\n\n”; sleep 3; doneOr, with a system process or command, for example, monitoring files in a directory: # while : ; do ls -l /var/tmp|while read line; do echo “$line”; sleep 0.4; done; echo -e “\n\n”; sleep 3; done

http://www.shayanderson.com/linux/read-file-or-system-process-and-continually-scroll-output-lines-with-linux-command-line-script-readr.htm