Project.pm: do not unlink nonexistent file
[girocco.git] / toolbox / edit-user-email.sh
blob00a92f9e0b8942c374488f0bf9a58b0b3a126684
1 #!/bin/sh
3 set -e
5 . @basedir@/shlib.sh
7 if [ -z "$1" ] || [ -z "$2" ] || [ -z "$3" ]; then
8 echo "Usage: $0 <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" ] && [ "$cfg_update_pwd_db" != "0" ]; then
35 "$cfg_basedir/bin/update-pwd-db" "$ETC/passwd" "$1"
37 echo "All changed lines listed above."