简单库存数据库的 bash 脚本

简单库存数据库的 bash 脚本

作为练习,我需要创建一个简单的 bash 脚本,可以在其中添加或删除清单文件中的项目。

要求如下:

每个项目可以具有以下属性

  • 身份证(必填)
  • 添加日期(必填)
  • 姓名(必填)
  • 类型(必填)
  • 笔记
  • 删除日期

添加一个项目

  • 从命令行读取项目属性“名称”、“类型”和“注释”,并
  • 生成“ID”和“添加日期”
  • 将新商品添加到库存中

通过 ID 删除项目

  • 设置删除日期
  • 使其不会显示在库存列表中

库存清单显示以下列

  • ID
  • 类型
  • 姓名
  • 添加日期
  • 笔记
  • 删除日期(如果您实现显示已删除项目的功能)

这就是我所做的:

创建一个名为数据库

touch database

创建一个名为库存.sh

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


echo
        read choice

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

*) echo Invalid choice - Bye.

                esac

创建一个名为添加记录.sh

echo Please enter item name:
read item
echo

        grep -q $item /home/dagobert/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'
echo 'c = Show inventory'
echo 'd = Exit database'

echo
        read choice

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

*) echo Invalid choice - Bye.

                esac

创建一个名为删除记录.sh

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

        grep -q $item /home/dagobert/Desktop/Assignments/A8/database
        if [ $? -eq 0 ]
        then
        echo
        sed -i '/'$item'/d' /home/dagobert/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'
echo 'c = Show inventory'
echo 'd = Exit database'

echo
        read choice

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

*) echo Invalid choice - Bye.

                esac

创建一个名为列表记录.sh

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

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

echo
        read choice

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

*) echo Invalid choice - Bye.

                esac

添加和删​​除项目就像魅力一样,但有一个要求不匹配。如果我删除一个项目,它就会从文件数据库中完全消失,而不是保留记录。

如何改进脚本,使已删除的项目仍保留在文件数据库中,并将删除日期添加到其中?

预先感谢 FreeRunner 的每一个帮助和建议

相关内容