66 lines
1.8 KiB
Bash
66 lines
1.8 KiB
Bash
#!/bin/bash
|
|
|
|
# Function to display usage information
|
|
usage() {
|
|
echo "Usage: $0 -a \"serial,model,capacity,TBW,smart\""
|
|
exit 1
|
|
}
|
|
|
|
# Check if the correct number of arguments is provided
|
|
if [ "$#" -ne 2 ]; then
|
|
usage
|
|
fi
|
|
|
|
# Parse command-line arguments
|
|
while getopts ":a:" opt; do
|
|
case ${opt} in
|
|
a)
|
|
# Extract the comma-separated list of attributes
|
|
IFS=',' read -ra ATTRS <<< "$OPTARG"
|
|
;;
|
|
\?)
|
|
echo "Invalid option: $OPTARG" 1>&2
|
|
usage
|
|
;;
|
|
esac
|
|
done
|
|
shift $((OPTIND -1))
|
|
|
|
# Check if the database file exists, otherwise create it and initialize the table
|
|
DB_FILE="drive_records.db"
|
|
if [ ! -f "$DB_FILE" ]; then
|
|
sqlite3 "$DB_FILE" <<EOF
|
|
CREATE TABLE drive_records (id INTEGER PRIMARY KEY, serial TEXT NOT NULL, model TEXT NOT NULL, capacity TEXT NOT NULL, TBW TEXT NOT NULL, smart TEXT NOT NULL);
|
|
EOF
|
|
fi
|
|
|
|
# Read the input data from stdin
|
|
while read -r line; do
|
|
# Extract values for each attribute and insert into the database
|
|
VALUES=()
|
|
IFS=' ' read -ra PARTS <<< "$line"
|
|
for ((i = 0; i < ${#ATTRS[@]}; i++)); do
|
|
case "${ATTRS[i]}" in
|
|
serial)
|
|
VALUES+=("${PARTS[0]}")
|
|
;;
|
|
model)
|
|
VALUES+=("${PARTS[1]}")
|
|
;;
|
|
capacity)
|
|
VALUES+=("${PARTS[2]}")
|
|
;;
|
|
TBW)
|
|
VALUES+=("${PARTS[3]}")
|
|
;;
|
|
smart)
|
|
VALUES+=("${PARTS[4]}")
|
|
;;
|
|
esac
|
|
done
|
|
|
|
# Insert the values into the database
|
|
sqlite3 "$DB_FILE" <<EOF
|
|
INSERT INTO drive_records (serial, model, capacity, TBW, smart) VALUES ('${VALUES[0]}', '${VALUES[1]}', '${VALUES[2]}', '${VALUES[3]}', '${VALUES[4]}');
|
|
EOF
|
|
done <<< "$(cat -)" |