I want to read 4 columns, namely UID, GID, username and home directory of specific users from /etc/passwd
into an SQLite database. I found this to create many subprocesses, and I'd like to have some improvement suggestions.
#!/bin/sh
db="$PWD/users.db"
users=$(grep -E 'joe|bill|tom' /etc/passwd)
sqlite3 "$db" <<EOF
DROP TABLE Users;
CREATE TABLE Users \
(UID INTEGER PRIMARY KEY, GID INTEGER, Username TEXT, Home TEXT);
EOF
echo "$users" | while IFS= read -r line
do
sqlite3 "$db" \
"INSERT INTO Users VALUES ( \
$(echo "$line" | cut -d":" -f3), \
$(echo "$line" | cut -d":" -f4), \
'$(echo "$line" | cut -d":" -f1)', \
'$(echo "$line" | cut -d":" -f6)');"
done