Shell Script Searches For A Single Word Pattern Recursively

in Categories Academic last updated May 5, 2008
#!/bin/bash
# Write a shell script that searches for a single word pattern recursively
# in the current working directory and display the numbers of times it occurred.
# -------------------------------------------------------------------------
# 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 one word pattern : "
read pattern
# wc  - word count
COUNT=$(grep -wr "${pattern}" . 2> /dev/null | wc -l)
echo "${pattern} occurred ${COUNT} times."

Shell Script that outputs the number of users with entries in the /etc/passwd file

in Categories File-management last updated April 9, 2008
#!/bin/bash
# Shell Script that outputs the number of users with entries in the
# Unix or Linux /etc/passwd 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.
# -------------------------------------------------------------------------
PASSWD=/etc/passwd
 
# easy method with wc
OUT=$(wc -l $PASSWD)
 
# More accurate method to find out
# If user account suspended using # character then second method 
# is more accurate and recommend
OUT1=$(grep -Ev "^#" $PASSWD | wc -l)
 
echo "$OUT [$OUT1]"
# You can filter result with awk
echo $OUT | awk '{ print $1 }'

Shell Script to display current date, calendar, and the number of user logged

in Categories Academic last updated April 3, 2008

This script use the concept of command substitution. It allows allows the output of a command to replace the command name. There are two forms:

$(command)

or

`command`

Bash shell performs the expansion by executing command and replacing the command substitution with the standard output of the command, with any trailing newlines deleted. Embedded newlines are not deleted, but they may be removed during word splitting.

For example you can execute date command to display todays date and then set a variable called NOW:
NOW=$(date)
or
NOW=`date`
To display back variable value simply run echo command:
echo $NOW

Sample Shell Script

#!/bin/bash
# Shell script to display current date, calendar, and the number of
# user logged.
# -------------------------------------------------------------------------
# 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 "Today is $(date)"
echo ""
 
echo "Calendar :"
cal
 
echo "Number of users currently logged : $(who | wc -l)"