#!/bin/bash # copyright 2007 Gilbert Ashley # BashTrix sort is an implementation of the 'sort' command # written in pure shell. Very few sort options are supported. # It's based on the combsort algorithm and was originally # written in BASH by Carl McNealy as a numerical sorter. VERSION=0.1 ERROR=0 # show program usage show_usage() { echo "Usage: ${0##/*} [OPTION]... [FILE]..." echo "Try '${0##/*} --help' for more information." exit $ERROR } # show program help show_help() { echo echo "Usage: ${0##/*} [OPTION]... [FILE]..." echo " or: (cat|echo) | ${0##/*} [OPTION]..." echo "Sort lines of each FILE and print the result to standard output." echo "With no FILE, read standard input." echo " --help display this help and exit" echo " --version output version information and exit" exit $ERROR } show_version() { echo ${0##/*}" (BashTrix) $VERSION" echo "Copyright 2007 Gilbert Ashley " echo "This is free software written in pure shell." exit $ERROR } # get the - options DUMMY for WORD in "$@" ; do case $WORD in -*) true ; case $WORD in -v) REVERSE_MATCH=1 ; shift ;; -n) PRINT_LINE_NUMBERS=1 ; shift ;; -H) PRINT_FILE_NAMES=1 ; shift ;; --help) show_help ;; --version) show_version ;; -) READ_STDIN=1 ; shift ;; esac ;; esac done function newGap { typeset -i newgap newgap=($1*10) newgap=$newgap/13 if [[ $newgap -eq 9 || $newgap -eq 10 ]] then newgap=11 elif [[ $newgap -lt 1 ]] then newgap=1 fi return $newgap } function swap { temp=${LINE[$1]} LINE[$1]=${LINE[$2]} LINE[$2]=$temp } ignoreme(){ # declare -a a=(1 6 5 7 8 9 3 2 4 0) #declare -a a=("Gh" "GH" G N B D F O Z U 1 10 2 3) declare -a a=("$@") echo "${#a[@]} Original values are ${a[@]} " let aSize=${#a[@]} let gap=$aSize while(true) do newGap $gap gap=$? let swapped=0 let i=0 let diff=$aSize-$gap while(true) do if [[ $i -ge $diff ]] then break fi let j=$i+$gap if [[ "${a[$i]}" > "${a[$j]}" ]] then swap $i $j let swapped=1 fi let i=i+1 done echo "gap=$gap" if [[ $gap -eq 1 && $swapped -eq 0 ]] then break 2 fi done echo "New values are ${a[@]} " } # from rev 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++ )) declare -a LINE[$FILE_LINE_COUNT]=$LINE done <"$1" fi #let aSize=${#a[FILE_LINE_COUNT]} let aSize=$FILE_LINE_COUNT let gap=$aSize while(true) do newGap $gap gap=$? let swapped=0 let i=0 let diff=$aSize-$gap while(true) ; do if [[ $i -ge $diff ]] ; then break fi let j=$i+$gap if [[ "${LINE[$i]}" > "${LINE[$j]}" ]] ; then swap $i $j let swapped=1 fi let i=i+1 done #echo "gap=$gap" if [[ $gap -eq 1 && $swapped -eq 0 ]] ; then break 2 fi done # go to next FILE in $@ shift done echo $FILE_LINE_COUNT # print the lines in reverse order while [[ ${FILE_LINE_COUNT} -gt 0 ]] ; do echo $FILE_LINE_COUNT:${LINE[$FILE_LINE_COUNT]} (( FILE_LINE_COUNT-- )) done # print the lines in foward order while [[ ${FILE_LINE_COUNT} -le ${aSize} ]] ; do echo $FILE_LINE_COUNT:${LINE[$FILE_LINE_COUNT]} (( FILE_LINE_COUNT++ )) 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