Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- while [ ! -f $file ]
- do
- #ask user for filename
- read -p "What is the filename? " username
- if [ ! -f $file ]
- then
- echo "File not found!"
- else
- echo "File found!"
- fi
- done
- #process each line and make a user from the data
- cat $file | while read line
- do
- #line example:
- #tony.stark@stark.com;1970/05/29;sudo,avengers;/avengersData
- #extract first name
- name=`echo $line | sed 's/^(.*)..*@.*$/1/g'`
- #name: tony
- #extract surname
- surname=`echo $line | sed 's/^.*.(.*)@.*$/1/g'`
- #surname: stark
- #extract birthdate
- bdate=`echo $line | sed 's/^.*;(.*);.*;.*$/1/g'`
- #bdate: 1970/05/29
- #extract list groups to add into array
- groups=`echo $line`
- #groups: sudo, avengers
- #extract name of shared folder
- folder=`echo $line`
- #folder: /avengersData
- #user = first letter of name, and full surname
- #get first letter
- temp1 = ${name:0:1}
- #build username
- user="${temp1}${surname}"
- #user: tstark
- #pass must be first month and year of birthdate arranged as MMYYYY
- passTemp1 = ${bdate:0:4}
- passTemp2 = ${bdate:6:7}
- #build password
- password = "${passTemp2}${passTemp1}"
- #password: 051970
- #tokenise group + add to array
- declare -a groupArray
- IFS=','
- groupArray=(`echo $groups | tr "," " "`)
- #create groups if not existing.
- for i in ${groupArray[@]}
- do
- if [ getent group $i ]
- then
- echo "group exists"
- else
- groupadd $i
- fi
- done
- #Create shared folders if not existing.
- if [ ! -d $folder ]
- then
- mkdir -p $folder
- fi
- #Create groups for shared folders
- gname=`echo "${folder:1}"`
- groupadd $gname
- #Set group as owner of directory and change permissions
- chgrp -R $gname $folder
- chmod -R 770 $folder
- #create user and add to groups
- if [ grep "^${user}:" /etc/passwd ]
- then
- echo "user exists already!"
- else
- #Create user
- useradd -m -d /home/$user -p $pass $user
- #Add user to groups
- for i in ${groupArray[@]}
- do
- usermod -a -G $i $user
- done
- #Add user to shared group
- usermod -a -G $gname $user
- fi
- #force password change
- chage -d 0 $user
- done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement