#!/bin/bash # copyright 2007 Gilbert Ashley # BashTrix tail is an implementation of the 'rev' command # written in pure shell. rev takes no options. VERSION=0.1 # show program usage show_usage() { echo echo "Usage: ${0##/*} [FILE]..." echo " or: (cat|echo) | ${0##/*} [FILE]... " echo "Reverse lines of a file." echo "The rev utility copies the specified files to the standard output," echo "reversing the order of characters in every line." echo "With no FILE specified, standard input is read." echo " --help display this help and exit" echo " --version output version information and exit" exit } show_version() { echo ${0##/*}" (BashTrix) $VERSION" echo "Copyright 2007 Gilbert Ashley " echo "This is free software written in pure shell." exit } # show usage if '-h' or '--help' is the first argument or no argument is given case $1 in "-h"|"--help") show_usage ;; "--version") show_version ;; esac for WORD in "$@" ; do case $WORD in -*) true ; case $WORD in --help) show_usage ;; --version) show_version ;; --) READ_STDIN=1 ; shift ;; esac ;; esac done if [[ $# -gt 0 ]] ; then while [[ $# -gt 0 ]] ; do FILE_NAME="$1" if [ ! -r "$1" ] ; then echo "Cannot find file $1" 1>&2 exit 1 else FILE_LINE_COUNT=0 LINE= IFS= while read LINE ; do # add the curent line to the line counter (( FILE_LINE_COUNT++ )) OUTPUT_STRING= STRING=$LINE while [[ "$STRING" != "" ]] ; do OUTPUT_STRING=${STRING:0:1}$OUTPUT_STRING STRING=${STRING:1} done echo $OUTPUT_STRING done <"$1" fi # go to next FILE in $@ shift done else # piped input is presumed to be separated into lines already FILE_NAME="STDIN" # output the requested lines in the file FILE_LINE_COUNT=0 LINE= IFS= while read LINE ; do # add the curent line to the line counter (( FILE_LINE_COUNT++ )) OUTPUT_STRING= STRING=$LINE while [[ "$STRING" != "" ]] ; do OUTPUT_STRING=${STRING:0:1}$OUTPUT_STRING STRING=${STRING:1} done echo $OUTPUT_STRING # go to next line in stdin shift done fi