Skip to content

Instantly share code, notes, and snippets.

@MechMK1
Last active October 22, 2016 13:15
Show Gist options
  • Select an option

  • Save MechMK1/6838928ffe79c4ab209be42401f70421 to your computer and use it in GitHub Desktop.

Select an option

Save MechMK1/6838928ffe79c4ab209be42401f70421 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Skript: m122_Scripts/SQLTux.sh
DBFILE=linuxusers.db
PASSWDFILE=/etc/passwd
TABLENAME="LinuxBenutzer"
#Stop if the password file is not readable
if ! [ -r "$PASSWDFILE" ]
then
echo "Could not read $PASSWDFILE. Exiting..."
exit 1
fi
#Clean the database file and create the structure new
echo "Preparing database file $DBFILE now..."
sqlite3 "$DBFILE" "DROP TABLE IF EXISTS $TABLENAME;"
sqlite3 "$DBFILE" "CREATE TABLE $TABLENAME (username text not null, uid integer not null, gid integer not null, homedir text);"
echo "Database prepared, filling..."
#Grep all users who are either root, start with 'vm' or or end with 'tux'
#Cut out the username, user id, group id and home directory
ALL="$(grep -i -E "^(root|vm[-_a-z0-9]+|[-_a-z0-9]+tux):" "$PASSWDFILE" | cut -d':' -f 1,3,4,6)"
#Print the data linewise and insert everything into the database
echo "$ALL" | while read line
do
_USER="$(echo "$line" | cut -d':' -f1)"
_UID="$(echo "$line" | cut -d':' -f2)"
_GID="$(echo "$line" | cut -d':' -f3)"
_HOMEDIR="$(echo "$line" | cut -d ':' -f4)"
sqlite3 "$DBFILE" "INSERT INTO $TABLENAME VALUES ('$_USER', $_UID, $_GID, '$_HOMEDIR');"
done
echo "Database filled. Printing results:"
sqlite3 -header "$DBFILE" "SELECT username, gid, uid, homedir FROM $TABLENAME;";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment