script for using a template account for multi-user environments
This commit is contained in:
parent
2015151a61
commit
91e73a0f9d
1 changed files with 95 additions and 0 deletions
95
convert-template-user.sh
Executable file
95
convert-template-user.sh
Executable file
|
@ -0,0 +1,95 @@
|
|||
#!/bin/sh
|
||||
# this script removes all data of the specified user and puts the data
|
||||
# of a template user in his directory.
|
||||
# All files of this template user will be scanned for the name of the
|
||||
# template user (case sensitivly) and every occourence will be replaced
|
||||
# by the name of the new user.
|
||||
# Furthermore every filename that includes the name of the template user
|
||||
# will be renamed to contain the name of the new user.
|
||||
# Finally all links, that contain the name of the template user will be
|
||||
# renamed to according to the new username.
|
||||
# This can help to create new users according to a template.
|
||||
#
|
||||
# how to use it:
|
||||
# 1) create a template user (with a long and unusual name, that would not
|
||||
# occour within text or filenames by coincidence)
|
||||
# 2) adapt all settings of this template user to your needs (proxies, Desktop, ...)
|
||||
# 3) create a new real user with the tool of your choice (adduser, yast, ...)
|
||||
# 4) run the script - e.g. "convert-user.sh --yes-remove-user-data NEW_USER"
|
||||
# 5) done!
|
||||
#
|
||||
# for every new user you should repeat steps 3 to 5
|
||||
#
|
||||
# BEWARE: all files of every new user will be DELETED without further warning!!!
|
||||
#
|
||||
# whoever wants to translate the error messages: just do it!
|
||||
#
|
||||
|
||||
|
||||
set -ue
|
||||
|
||||
HOME_DIR=/home
|
||||
MUSTER=musternutzer
|
||||
|
||||
### funktionen ###
|
||||
|
||||
abbruch()
|
||||
{
|
||||
echo "Fehler: $2"
|
||||
exit $1
|
||||
}
|
||||
|
||||
# pruefen, ob die Anzahl der Parameter passt
|
||||
[ $# -ne 2 ] && abbruch 1 "Als Parameter ist die Option '--yes-remove-user-data' und der Name des zu konvertierenden (existierenden) Nutzers erforderlich!"
|
||||
|
||||
# pruefen, ob der erste Parameter "--yes-remove-user-data" ist
|
||||
[ "$1" != "--yes-remove-user-data" ] && abbruch 2 "Als Parameter ist die Option '--yes-remove-user-data' und der Name des zu konvertierenden (existierenden) Nutzers erforderlich!"
|
||||
NEU="$2"
|
||||
|
||||
[ -z "$NEU" ] && abbruch 10 "Der neue Nutzername ist leer"
|
||||
|
||||
[ -d "$HOME_DIR/$MUSTER" ] || abbruch 3 "der Musternutzer ($MUSTER) existiert nicht!"
|
||||
|
||||
[ -d "$HOME_DIR/$NEU" ] || abbruch 4 "der zu konvertierende Nutzer ($NEU) existiert nicht!"
|
||||
|
||||
[ "$NEU" != "`basename $NEU`" ] && abbruch 5 "der zu konvertierende Nutzername ($NEU) ist ungueltig!"
|
||||
|
||||
#echo "Letzte Chance zum Abbruch (STRG-C) ..."
|
||||
#echo "Enter zum Fortfahren ..."
|
||||
#read
|
||||
|
||||
echo "Entferne die alten Nutzerdaten ..."
|
||||
find "$HOME_DIR/$NEU" -mindepth 1 -maxdepth 1 -xdev | while read a
|
||||
do rm -rf "$a"
|
||||
done || abbruch 6 "das alte Nutzerverzeichnis konnte nicht geloescht werden!"
|
||||
|
||||
echo "Kopiere die Daten des Musternutzers ..."
|
||||
cp -a "$HOME_DIR/$MUSTER/." "$HOME_DIR/$NEU" || abbruch 7 "die Dateien des Musternutzers konnten nicht kopiert werden!"
|
||||
|
||||
echo "Passe die Daten des Musternutzers an ..."
|
||||
find "$HOME_DIR/$NEU" -type f | while read a
|
||||
do sed -i "s/$MUSTER/$NEU/g" "$a" || abbruch 8 "die Anpassung der Datei $a schlug fehl!"
|
||||
done
|
||||
|
||||
echo "Passe die Links an, deren Ziele den Nutzernamen enthalten ..."
|
||||
find "$HOME_DIR/$NEU" -type l | while read a
|
||||
do ZIEL=`readlink "$a"`
|
||||
ZIEL_SUBST=`echo "$ZIEL" | sed "s/$MUSTER/$NEU/g"`
|
||||
[ "$ZIEL" != "$ZIEL_SUBST" ] && ln -sfn "$ZIEL_SUBST" "$a"
|
||||
done
|
||||
|
||||
echo "Passe Dateinamen an, die den Nutzernamen beinhalten ..."
|
||||
find "$HOME_DIR/$NEU" | while read a
|
||||
do NAME=`basename "$a"`
|
||||
NAME_SUBST=`echo "$NAME" | sed "s/$MUSTER/$NEU/g"`
|
||||
[ "$NAME" != "$NAME_SUBST" ] && mv "$a" "`dirname \"$a\"`/$NAME_SUBST"
|
||||
done
|
||||
|
||||
USER_INFO=`ls -ldn "$HOME_DIR/$NEU" | cut -d " " -f 4,5 | sed 's/ /./'`
|
||||
#echo "Neuer Besitzer wird sein: $USER_INFO"
|
||||
|
||||
if [ "$(id -u)" = "0" ]
|
||||
then chown -R "$USER_INFO" "$HOME_DIR/$NEU" || abbruch 9 "der Nutzer ($NEU) konnte nicht als Eigentuemer seines Home-Verzeichnisses gesetzt werden!"
|
||||
else echo
|
||||
echo "Nun muessen die Dateien noch dem neuen Benutzer uebergeben werden ..."
|
||||
fi
|
Loading…
Reference in a new issue