A8 – Linux Bash Project

Assignment Series #A8 – Journeyman’s Piece

We will write a little program that keeps track of our IT inventory. We want to be able to:

  • show our inventory
  • add an item to the inventory
  • remove items from the inventory

Here are the detailed requirements:

  • generate the file if it doesn’t exist yet
  • each item can have the following properties
    • ID (mandatory)
    • date added (mandatory)
    • name (mandatory)
    • type (mandatory)
    • notes
    • date removed

adding an item

  • reads item properties “name”, “type”, and “notes” from the command line and
  • generates “ID” and “date added”
  • adds the new item to the inventory

removing an item by ID

  • sets the date it was removed
  • keeps it from being shown in the inventory listing

the inventory listing shows the following columns

  • ID
  • type
  • name
  • date added
  • notes
  • date removed (if you implement the functionality to show removed items)

My solution

For my project I’ll create 6 files:

  • databse
  • inventory.sh
  • add_record.sh
  • delete_record.sh
  • mark_record.sh
  • list_record.sh

1. Create database file

touch database

2. Create main program (inventory.sh)

#!/bin/bash
# Author: Cybercop
# Date: 16/02/21
# Description: This script will show the program options
# Modified: 08/03/21
#!/bin/bash

echo
echo Please select one of the following options:
echo
echo 'a = Add a record'
echo 'b = Delete a record permanently'
echo 'c = Mark a record as removed'
echo 'd = Show inventory'
echo 'e = Exit database'

echo
        read choice

                case $choice in
a) /home/hacker/Desktop/Assignments/A8/add_record.sh;;
b) /home/hacker/Desktop/Assignments/A8/del_record.sh;;
c) /home/hacker/Desktop/Assignments/A8/mark_record.sh;;
d) /home/hacker/Desktop/Assignments/A8/list_record.sh;;
e) exit;;

*) echo Invalid choice - Bye.

                esac

3. Script to add a record (add_record.sh)

#!/bin/bash
# Author: Cybercop
# Date: 16/02/21
# Description: This script will add system inventory to database
# Modified: 08/03/21

echo Please enter item name:
read item
echo

        grep -q $item /home/hacker/Desktop/Assignments/A8/database
        if [ $? -eq 0 ]
        then
        echo ERROR -- Item $item already exist
        echo
        exit 0
        fi

echo Please enter item type:
read type
echo

echo Please enter Description:
read description
echo

echo "ID:$RANDOM" "NAME:$item" "CATEGORY:$type" "NOTES:$description" "Date added:$(date)" >> database

echo "The provided item has been added"
echo

echo
echo Please select one of the following options:
echo
echo 'a = Add a record'
echo 'b = Delete a record permanently'
echo 'c = Mark a record as removed'
echo 'd = Show inventory'
echo 'e = Exit database'

echo
        read choice

                case $choice in
a) /home/hacker/Desktop/Assignments/A8/add_record.sh;;
b) /home/hacker/Desktop/Assignments/A8/del_record.sh;;
c) /home/hacker/Desktop/Assignments/A8/mark_record.sh;;
d) /home/hacker/Desktop/Assignments/A8/list_record.sh;;
e) exit;;

*) echo Invalid choice - Bye.

                esac

4. Script to delete a record permanently (delete_record.sh)

#!/bin/bash
# Author: Cybercop
# Date: 16/02/21
# Description: This script will add system inventory to database
# Modified: 08/03/21

echo Please enter ID or the itemname you want to delete?
read item
echo

        grep -q $item /home/hacker/Desktop/Assignments/A8/database
        if [ $? -eq 0 ]
        then
        echo
        sed -i '/'$item'/d' /home/hacker/Desktop/Assignments/A8/database
        echo $item has been deleted
        else
        echo Record does not exist
        fi
echo
echo Please select one of the following options:
echo
echo 'a = Add a record'
echo 'b = Delete a record permanently'
echo 'c = Mark a record as removed'
echo 'd = Show inventory'
echo 'e = Exit database'

echo
        read choice

                case $choice in
a) /home/hacker/Desktop/Assignments/A8/add_record.sh;;
b) /home/hacker/Desktop/Assignments/A8/del_record.sh;;
c) /home/hacker/Desktop/Assignments/A8/mark_record.sh;;
d) /home/hacker/Desktop/Assignments/A8/list_record.sh;;
e) exit;;

*) echo Invalid choice - Bye.

                esac

5. Script to mark a record as deleted

#!/bin/bash
# Author: Cybercop
# Date: 16/02/21
# Description: This script will add system inventory to database
# Modified: 08/03/21

echo Please enter ID or the itemname you want to delete?
read item
echo

        grep -q $item /home/hacker/Desktop/Assignments/A8/database
        if [ $? -eq 0 ]
        then
        echo
        sed -i '/'$item"/s/\$/ Date deleted:$(date)/" /home/hacker/Desktop/Assignments/A8/database
        echo $item is marked as removed
        else
        echo Record does not exist
        fi

echo
echo Please select one of the following options:
echo
echo 'a = Add a record'
echo 'b = Delete a record permanently'
echo 'c = Mark a record as removed'
echo 'd = Show inventory'
echo 'e = Exit database'

echo
        read choice

                case $choice in
a) /home/hacker/Desktop/Assignments/A8/add_record.sh;;
b) /home/hacker/Desktop/Assignments/A8/del_record.sh;;
c) /home/hacker/Desktop/Assignments/A8/mark_record.sh;;
d) /home/hacker/Desktop/Assignments/A8/list_record.sh;;
e) exit;;

*) echo Invalid choice - Bye.

                esac

6. Script to list records (list_record.sh)

#!/bin/bash
# Author: Cybercop
# Date: 16/02/21
# Description: This script will add system inventory to database
# Modified: 08/03/21
#!/bin/bash

clear
cat /home/hacker/Desktop/Assignments/A8/database  

echo
echo Please select one of the following options:
echo
echo 'a = Add a record'
echo 'b = Delete a record permanently'
echo 'c = Mark a record as removed'
echo 'd = Show inventory'
echo 'e = Exit database'


echo
        read choice

                case $choice in
a) /home/hacker/Desktop/Assignments/A8/add_record.sh;;
b) /home/hacker/Desktop/Assignments/A8/del_record.sh;;
c) /home/hacker/Desktop/Assignments/A8/mark_record.sh;;
d) /home/hacker/Desktop/Assignments/A8/list_record.sh;;
e) exit;;

*) echo Invalid choice - Bye.

                esac

 

Final notes

  • You can’t add two items with the same name. If you try you’ll get an error message.
  • The ID for each record will be generated randomly $Random
  • A timestamp will be added for every item $(date)
  • You can choose between deleting a record permanently or mark it as deleted with a timestamp $(date)

To get all the scripts to work I need to give them execution permissions chmod +x