#!/bin/bash # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # Copyright (c) 2010 FSOCA final project | http://joaosantacruz.com # This script is licensed under GNU GPL version 2.0 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # Create a BASH script (word-counter.sh) that receives a text file and reports # how many words are in that file. Extension: Also report how many lines the file has. Also # work out how many words the script itself has and add it to the number of words in the text # file. Display the total number of words. # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - clear echo "= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =" echo " Word Counter " echo "= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =" echo "" if [ -f "$1" ] then wcounter=`cat $1 | wc -w | tr -d ' '` # return the number of words in the file and remove white spaces lcounter=`cat $1 | wc -l | tr -d ' '` # return the number of lines in the file and remove white spaces echo "File '$1' has $wcounter words and $lcounter lines." this_wcounter=`cat $0 | wc -w | tr -d ' '` # return the number of words in the file and 'trim' to remove wspaces this_lcounter=`cat $0 | wc -l | tr -d ' '` # return the number of lines in the file and 'trim' to remove wspaces echo "The script itself has $this_wcounter words and $this_lcounter lines." wtotal=$(($wcounter+$this_wcounter)) echo "The total is $wtotal words." else echo "'$1' is not a file!" fi echo "" echo "= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =" echo "Press [enter] key to continue. . ."; read pressedKey