Tuesday, December 25, 2012

Bash Shell Scripting example project "library management"



This program is a sample shell script. To understand this project you must know the basic syntax of shell programming. In this article I tried to show an example how we can use a shell as a programming language. I have build the very simple shell script for managing a library books. If you are completely new to shell programming in Linux then at search the following topic on Google about their syntax and uses before proceeding this article. List of topic are:
  1. variables
  2. if condition
  3. switch case
  4. printf ,read , echo command
  5. while loop, for loop
  6. grep command
  7. function declaration
Now if you have learn above topic in shell programming then you can understand this project library management. In this project simple features are included like adding books, editing books information,remove and search books and view all books. Lets start the project.

Step-1:
Now ensure that some global variables that you will be using throughout the script are set up. Set the record files and a temporary file, and trap Ctrl + C , so that your temporary file will be removed if the user interrupts the script:
menu_choice=""
record_file="bookRecords.ldb"
temp_file=/tmp/ldb.$$
trap 'rm -f $temp_file' EXIT
Step-2:
Define your functions, so that the script, executing from the top lines, can find all the function definitions before you attempt to call any of them for the first time. To avoid rewriting the same code in several places, the first two functions are simple utilities:
get_return(){
printf '\tPress return\n'
read x
return 0
}

get_confirm(){
printf '\tAre you sure?\n'
while true
do 
  read x
  case "$x" in
      y|yes|Y|Yes|YES)
          return 0;;
      n|no|N|No|NO)
          printf '\ncancelled\n'
          return 1;;
      *) printf 'Please enter yes or no';;
  esac
done
}
Step-3:
Here you come to the main menu function,set_menu_choice where option are displayed for user.
set_menu_choice(){
clear
printf 'Options:-'
printf '\n'
printf '\ta) Add new Books records\n'
printf '\tb) Find Books\n'
printf '\tc) Edit Books\n'
printf '\td) Remove Books\n'
printf '\te) View Books\n'
printf '\tf) Quit\n'
printf 'Please enter the choice then press return\n'
read menu_choice
return
}
Step-4:
Here one very short function insert_record , for adding to the database files which is used by the large function add_books. In this function user are requested to provide the books information and store in file using insert_record function.
insert_record(){
echo $* >>$record_file
return
}


#!!!!!!!!!...........................!!!!!!!!!!!!!!!!
#This function ask user for details information about book for keeping records

add_books(){

#prompt for information

printf 'Enter Books category:-'
read tmp
liCatNum=${tmp%%,*}

printf 'Enter Books title:-'
read tmp
liTitleNum=${tmp%%,*}

printf 'Enter Auther Name:-'
read tmp
liAutherNum=${tmp%%,*}

#Check that they want to enter the information
printf 'About to add new entry\n'
printf "$liCatNum\t$liTitleNum\t$liAutherNum\n"

#If confirmed then append it to the record file
if get_confirm; then
   insert_record $liCatNum,$liTitleNum,$liAutherNum
fi

return
}
Step-5:
The find_books function searches for the catalog name text in the books title file, using the
grep command. We need to know how many times the string was found, but grep only returns
a value telling us if it matched zero times or many. To get around this, we store the
output in a file, which will have one line per match, then count the lines in the file.
The word count command, wc, has white-space in its output, separating the number of lines,
words and characters in the file. We use the $(wc -l $temp_file) notation to extract the
first parameter from the output to set the linesfound variable. If we wanted another,
later parameter we would use the set command to set the shell's parameter variables to
the command output.
find_books(){
grep computer $record_file > $temp_file
   
  set $(wc -l $temp_file)
  linesfound=$1

  case "$linesfound" in
  0)    echo "Sorry, nothing found"
        get_return
        return 0
        ;;
  *)    echo "Found the following"
        cat $temp_file
        get_return
        return 0
  esac
return
}
Step-6:
remove_books allow the use to remove the book information one at a time from the database files. We use grep -v to invert the search and select the non matching lines . Notice we must use a temporary file where non matching lines are written then that temporary file was rename with original file.
 $temp_file

 set $(wc -l $temp_file)
   linesfound=$1

   case "$linesfound" in
   0)    echo "Sorry, nothing found\n"
         get_return
         return 0
         ;;
   *)    echo "Found the following\n"
         cat $temp_file ;;
        esac
 printf "Type the books titel which you want to delete\n"
 read searchstr

  if [ "$searchstr" = "" ]; then
      return 0
   fi
 grep -v "$searchstr" $record_file > $temp_file
 mv $temp_file $record_file
 printf "Book has been removed\n"
 get_return
return
}

view_books(){
printf "List of books are\n"

cat $record_file
get_return
return
}
Step-7:
Now two function view_books and edit_books are declared. Where view_books function allow user to view the all available books and edit_books function allow user to re-enter the books information.
edit_books(){

printf "list of books are\n"
cat $record_file
printf "Type the tile of book you want to edit\n"
read searchstr
  if [ "$searchstr" = "" ]; then
     return 0
  fi
  grep -v "$searchstr" $record_file > $temp_file
  mv $temp_file $record_file
printf "Enter the new record"
add_books

}
Step-8:
Now that all the functions have been defined, you can enter the main routine. The first few lines simply get the files into a known state; then you call the menu function, set_menu_choice, and act on the output.
When quit is selected, you delete the temporary file, write a message, and exit with a successful completion condition:
rm -f $temp_file
if [!-f $record_file];then
touch $record_file
fi

clear
printf '\n\n\n'
printf 'Mini library Management'
sleep 1

quit="n"
while [ "$quit" != "y" ];
do

#funtion call for choice
set_menu_choice
case "$menu_choice" in
a) add_books;;
b) find_books;;
c) edit_books;;
d) remove_books;;
e) view_books;;
f) quit=y;;
*) printf "Sorry, choice not recognized";;
esac
done
# Tidy up and leave

rm -f $temp_file
echo "Finished"

exit 0

NOTES ON THE APPLICATION
The trap command at the start of the script is intended to trap the user's pressing of Ctrl+c. This may be either the EXIT or the INT signal, depending on the terminal setup.

We will leave this and other improvements to your imagination and creativity, as you can modify the code.
You can also download the shell script with complete source code from here


4 comments:

  1. Can you please update the download link.

    ReplyDelete
  2. This whole script is to be put in one file or each portion is put in seprate script files?

    ReplyDelete