clone/update: optimize ref removal
[girocco.git] / toolbox / edit-user-email.sh
blob6c7a10524ee28bca0a8302064d06d0697d995306
1 #!/bin/sh
3 set -e
5 . @basedir@/shlib.sh
7 if [ -z "$1" -o -z "$2" -o -z "$3" ]; then
8 echo "Syntax: ./edit-user-email.sh <username> <old e-mail> <new e-mail>"
9 exit 1
11 ETC="$cfg_chroot/etc"
12 COUNT=`grep -E -c "^$1:" "$ETC/passwd"`
13 if [ "$COUNT" -ne "1" ]; then
14 echo "fatal: user '$1' doesn't appear to exist (or exists multiple times, or contains regexpy characters)."
15 exit 1
17 if echo "$2" | grep -q '[,:]'; then
18 echo "fatal: e-mail '$2' has illegal characters ([,:])."
19 exit 1
21 if ! echo "$3" | grep -E -q '^[a-zA-Z0-9+._-]+@[a-zA-Z0-9.-]+$'; then
22 echo "fatal: e-mail '$3' is not valid (^[a-zA-Z0-9+._-]+@[a-zA-Z0-9.-]+$)."
23 exit 1
25 sed \
26 -e "s/^$1\(:[^:]*:[^:]*:[^:]*:\)$2\([,:]\)/$1\\1$3\\2/" \
27 -e "t show" \
28 -e "b" \
29 -e ": show" \
30 -e "w /dev/stderr" \
31 "$ETC/passwd" > "$ETC/passwd.$$"
32 mv -f "$ETC/passwd.$$" "$ETC/passwd"
33 rm -f "$ETC/passwd.$$"
34 if [ -n "$cfg_update_pwd_db" -a "$cfg_update_pwd_db" != "0" ]; then
35 "$cfg_basedir/bin/update-pwd-db" "$ETC/passwd" "$1"
37 echo "All changed lines listed above."