This script also demonstrate how to use arrays under bash shell script.
#!/bin/bash # Bash shell script to reverse text file contain i.e. concatenate files and # print on the standard output in reverse. This script also demonstrate how # to use arrays under bash shell script. # ------------------------------------------------------------------------- # Copyright (c) 2005 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. # ------------------------------------------------------------------------- FILE="$1" if [ $# -eq 0 ]; then echo "$(basename $0) - file-name" exit 1 fi textArray[0]="" # hold text c=0 # counter # read whole file in loop while read line do textArray[$c]=$line # store line c=$(expr $c + 1) # increase counter by 1 done < $FILE # get length of array len=$(expr $c - 1 ) # use for loop to reverse the array for (( i=$len; i>=0; i-- )); do echo ${textArray[$i]} done
Featured Articles:
- 20 Linux System Monitoring Tools Every SysAdmin Should Know
- 20 Linux Server Hardening Security Tips
- 10 Greatest Open Source Software Of 2009
- My 10 UNIX Command Line Mistakes
- Top 5 Email Client For Linux, Mac OS X, and Windows Users
- Top 20 OpenSSH Server Best Security Practices
- Top 10 Open Source Web-Based Project Management Software
- Top 5 Linux Video Editor Software
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.
- Download Script
- Email this to a friend
- Rss Feed
- Last Updated: 07/24/08
{ 3 comments… read them below or add one }
textArray[c]=$line # store line
isn’t it
textArray[$c]=$line # store line
^
|
ERROR
zakko,
Thanks for the heads up.
can’t you just do a
tac filename.txt > filename-reversed.txt