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 read 5 digit number and calculate the sum of digit

in Categories Shell Math last updated April 4, 2008
#!/bin/bash
# Shell script to read 5 digit number and calculate the sum of digit
# -------------------------------------------------------------------------
# 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
 
# find out length of string using wc -c command
len=$(echo $n | wc -c)
 
# remove \n i.e. new line character
len=$(( $len - 1 ))
 
# use loop to go throug all digit one by one and calculate sum of digit on fly
for (( i=1; i <= $len; i++ ))
do
   sum=$(( $sum + $(echo $n | cut -c $i) ))
done
echo "Sum of $n is $sum"

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)"