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 To Display Last Five Lines Of The File

in Categories Academic last updated April 12, 2008

Given the filename by the user as the input, write a shell script to display the last five lines of the file.

Shell Script

#!/bin/bash
# get filename
echo -n "Enter File Name : "
read fileName
 
# make sure file exits for reading
if [ ! -f $fileName ]; then
  echo "Filename $fileName does not exists"
  exit 1
fi
 
# display last five lines of the file using tail command
tail -5 $fileName

Command Line Argument

You can read file name as command line argument:

#!/bin/bash
# get filename
fileName="$1"
# make sure command line arg provided 
if [ -z $1 ]; then
	echo "Syntax: $(basename $0) filename"
	exit 1
fi
# make sure file exits for reading
if [ ! -f $fileName ]; then
  echo "Filename $fileName does not exists"
  exit 1
fi
# ok display last five lines of the file using tail command
tail -5 $fileName

Shell Script accept password using read commnad

in Categories Script last updated April 10, 2008
#!/bin/bash
# Script accept password using read commnad
# Not *very secure*, this script is for learning purpose only 
# -------------------------------------------------------------------------
# This script is part of nixCraft shell script collection (NSSC)
# Visit http://bash.cyberciti.biz/ for more information.
# -------------------------------------------------------------------------
PASS="abc123"
read -s -p "Password: " mypassword
echo ""
[ "$mypassword" == "$PASS" ] && echo "Password accepted" || echo "Access denied"