s4

view scripts/add-user-csv.sh @ 545:7cd9e895fa09

Supply usage when no argument supplied
author HIROSE Yuuji <yuuji@gentei.org>
date Sat, 06 Apr 2019 20:05:35 +0900
parents 6039630ea2f5
children 1d0344a5f5aa
line source
1 #!/bin/sh
3 if [ -z "$1" ]; then
4 cat<<-EOF >&2
5 Usage: ./add-user-csv.sh CSVfile >&2
6 Csv file should be the form of:
7 username(email),notify-email,gecos,IniPassword
8 EOF
9 exit 1
10 fi
12 target=$(cd `dirname "$1"`; pwd)/`basename $1`
14 cd `dirname $0`/..
15 if ! . ./s4-funcs.sh; then
16 echo "Cannot find s4-funcs.sh, which should be located in $mydir/.."
17 exit 1
18 fi
20 sq $db<<-EOF |
21 .mode csv
22 CREATE TEMPORARY TABLE _newuser(user,email,gecos,inipswd);
23 .import $target _newuser
24 .mode list
25 .sepa ,
26 SELECT * FROM _newuser n
27 WHERE NOT EXISTS (SELECT * FROM user WHERE name=n.user);
28 EOF
29 while IFS=, read user email gecos inipswd
30 do
31 encpswd=`echo $inipswd|mypwhash`
32 case "$user" in
33 *\'*) echo "Skipping [$user]"
34 continue ;;
35 *@*) ;; # OK
36 *) echo "Skipping [$user] because of lacking email pattern" >&2
37 continue ;;
38 esac
39 # echo "$user" "$user" "$inipswd" "$encpswd"
40 query "INSERT INTO user VALUES('$user');" &&
41 echo Adding $user
42 dbsetbyid user "$user" email "$email"
43 dbsetbyid user "$user" gecos "$gecos"
44 dbsetbyid user "$user" pswd "$encpswd"
45 done