Shell Script To Count English Language Articles Such As 'A', 'An' and 'The'

in Categories Decision Making, File-management last updated April 21, 2008

This script also covers following techniques:
=> Reading input text file line by line
=> Reading and processing words in input
=> Convert word to lowercase

#!/bin/bash
# Write a shell script that counts English language articles (a, an, the) 
# in a given text file.
#
# --------------------------------------------------------------------
# This is a free shell script under GNU GPL version 2.0 or above
# Copyright (C) 2005 nixCraft project.
# Feedback/comment/suggestions : http://cyberciti.biz/fb/
# -------------------------------------------------------------------------
# This script is part of nixCraft shell script collection (NSSC)
# Visit http://bash.cyberciti.biz/ for more information.
# -------------------------------------------------------------------------
echo -n "Enter a file name : "
read file
a=0
the=0
an=0
 
# make sure file exist
if  [ ! -f $file ]
then
	echo "$file not a file!"
	exit 1
fi
 
# put while loop to read a $file
while read line
do
       #process each word 
	for w in $line 
	do
		# convert word to lowercase; so that we can count ThE, THE, the, THe etc all
		lword="$(echo $w | tr '[A-Z]' '[a-z]')"
 
		# is it 'a' article?
		[ $lword = "a" ] && (( a++ )) || :
		[ $lword = "the" ] && (( the++ )) || :
		[ $lword = "an" ] && (( an++ )) || :
	done
done < $file
 
# display stats
echo "a article occured $a times"
echo "the article occured $the times"
echo "an article occured $an times"

Monitor UNIX / Linux Server Disk Space with Shell Script

in Categories Monitoring last updated April 10, 2008

Shell script to monitor or watch the disk space and send an email alert if the (free avilable) percentage of space is >= 90%

#!/bin/sh
# Shell script to monitor or watch the disk space
# It will send an email to $ADMIN, if the (free avilable) percentage 
# of space is >= 90% 
# -------------------------------------------------------------------------
# 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.
# ----------------------------------------------------------------------
# Linux shell script to watch disk space (should work on other UNIX oses )
# SEE URL: http://www.cyberciti.biz/tips/shell-script-to-watch-the-disk-space.html
# set admin email so that you can get email
ADMIN="me@somewher.com"
# set alert level 90% is default
ALERT=90
df -H | grep -vE '^Filesystem|tmpfs|cdrom' | awk '{ print $5 " " $1 }' | while read output;
do
  #echo $output
  usep=$(echo $output | awk '{ print $1}' | cut -d'%' -f1  )
  partition=$(echo $output | awk '{ print $2 }' )
  if [ $usep -ge $ALERT ]; then
    echo "Running out of space \"$partition ($usep%)\" on $(hostname) as on $(date)" | 
     mail -s "Alert: Almost out of disk space $usep" $ADMIN
  fi
done

Bash shell script to reverse text file contain using Shell array

in Categories File-management last updated July 24, 2008

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