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"

Shell program to read two numbers and display all the odd numbers berween those two number

in Categories Academic, Decision Making last updated April 8, 2008
#!/bin/bash
# Shell program to read two numbers and display all the odd
# numbers berween those two number
# -----------------------------------------------
# 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.
# -------------------------------------------------------------------------
 
echo -n "Enter first number : "
read n1
 
echo -n "Enter second number : "
read n2
 
if [ $n2 -gt $n1 ];
then
  for(( i=$n1; i<=$n2; i++ ))
  do
    # see if it is odd or even number
    test=$(( $i % 2 ))
    if [ $test -ne 0 ]; 
    then
      echo $i
    fi   
  done
else
  echo "$n2 must be greater than $n1, try again..." 
fi

Shell script to read a number and write the number in words

in Categories Decision Making last updated April 5, 2008

Script logic is as follows:

a) First read number into a shell variable

b) Next get one digit at a time

c) Use simple case control structure and loop to find digit equivalent in words

Sample script to convert numbers into equivalent words

#!/bin/bash
# Shell script to read a number and write the number in words. Use case
# control structure. For example 12 should be print as one two
# -------------------------------------------------------------------------
# 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.
# -------------------------------------------------------------------------
 
echo -n "Enter number : "
read n
 
len=$(echo $n | wc -c)
len=$(( $len - 1 ))
 
echo "Your number $n in words : "
for (( i=1; i<=$len; i++ ))
do
   # get one digit at a time
    digit=$(echo $n | cut -c $i)
   # use case control structure to find digit equivalent in words 
    case $digit in
        0) echo -n "zero " ;;
        1) echo -n "one " ;;
        2) echo -n "two " ;;
        3) echo -n "three " ;;
        4) echo -n "four " ;;
        5) echo -n "five " ;;
        6) echo -n "six " ;;
        7) echo -n "seven " ;;
        8) echo -n "eight " ;;
        9) echo -n "nine " ;;
    esac	
done
 
# just print new line
echo ""

Menu Driven Shell Script

in Categories Decision Making last updated April 5, 2008

Explains how to write a menu driven Shell script, which has following options:
1. Contents of /etc/passwd
2. List of users currently logged
3. Present handling directory (working directory)
4. Exit

Take action as per selected option

#!/bin/bash
# A menu driven Shell script which has following options
# Contents of /etc/passwd
# List of users currently logged
# Prsent handling directory
# Exit
# As per option do the job
# -----------------------------------------------
# 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.
# -------------------------------------------------------------------------
 
while :
do
 clear
 echo "   M A I N - M E N U"
 echo "1. Contents of /etc/passwd"
 echo "2. List of users currently logged"
 echo "3. Prsent handling directory"
 echo "4. Exit"
 echo -n "Please enter option [1 - 4]"
 read opt
 case $opt in
  1) echo "************ Conents of /etc/passwd *************";
     more /etc/passwd;;
  2) echo "*********** List of users currently logged";
     who | more;;
  3) echo "You are in $(pwd) directory";   
     echo "Press [enter] key to continue. . .";
     read enterKey;;
  4) echo "Bye $USER";
     exit 1;;
  *) echo "$opt is an invaild option. Please select option between 1-4 only";
     echo "Press [enter] key to continue. . .";
     read enterKey;;
esac
done

Shell script to read a character (upper or lower), digit, special symbol and display message according to the character entered

in Categories Decision Making last updated April 5, 2008

This script uses sed command to find out all digits or upper characters. -z string (conditional expression) will return true if the length of string is zero.

Script logic

Read one character
[1] Use sed command to determine input character is digit or not (sed -e ‘s/[A-Z]//g’))

[2] Attempt to match regex [0-9] i.e. all digit against the pattern space

[3] If successful, replace that portion matched with replacement // i.e. do nothing.
If digits are in input, it will replace all those digit, and if input is only digit, nothing is left i.e. string is zero and that is what tasted with -z conditional expression.

#!/bin/bash
# Shell script to read a character (upper or lower), digit, special symbol and
# display message according to the character entered
# -----------------------------------------------
# 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.
# -------------------------------------------------------------------------
 
char=""
echo -n "Enter a one character : "
read char
# use sed command to determine input character is digit or not
# -e : start script
# s/[0-9]// : Attempt to match regex [0-9] i.e. all digit against the pattern space (echo $char).
# If  successful,  replace  that  portion  matched  with replacement i.e. // do nothing. 
# g : Global replacement
# so if digit are in input it will replace all those digit, and if input is only digit, nothing
# is left i.e. string is zero and that is what tasted with -z
if [ -z $(echo $char | sed -e 's/[0-9]//g') ]
then
	echo "$char is Number/digit"
elif [ -z $(echo $char | sed  -e 's/[A-Z]//g') ] # find out if character is upper
then
        echo "$char is UPPER character"
 
elif [ -z $(echo $char | sed -e 's/[a-z]//g') ] # find out if character is lower
then
        echo "$char is lower character"
else
	echo "$char is Special symbol" # else it is special character
fi

Shell script to read a number and find whether the number is odd or even

in Categories Decision Making last updated April 5, 2008
#!/bin/bash
# Shell script to read a number and find whether the number is odd or even
# -------------------------------------------------------------------------
# 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.
# -------------------------------------------------------------------------
 
echo -n "Enter numnber : "
read n
 
rem=$(( $n % 2 ))
 
if [ $rem -eq 0 ]
then
  echo "$n is even number"
else
  echo "$n is odd number"
fi

Shell Script to read source file and copy it to target file

in Categories Decision Making last updated April 5, 2008
#!/bin/bash
# Shell to read source file and copy it to target file. If the file
# is copied successfully then give message 'File copied successfully'
# else give message 'problem copying file'
# -------------------------------------------------------------------------
# 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.
# -------------------------------------------------------------------------
 
echo -n "Enter soruce file name : "
read src
echo -n "Enter target file name : "
read targ
 
if [ ! -f $src ]
then
	echo "File $src does not exists"
	exit 1
elif [ -f $targ ]
then
	echo "File $targ exist, cannot overwrite"
	exit 2
fi
 
# copy file 
cp $src $targ
 
# store exit status of above cp command. It is use to 
# determine  if shell command operations is successful or not
status=$?
 
if [ $status -eq 0 ]
then
	echo 'File copied successfully'
else
	echo 'Problem copuing file'
fi