Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- file=$1
- # Check that the user provided a file as the first argument
- if [[ ! -f $1 || -z $1 ]]
- then
- echo "No argument supplied. Please enter the file you wish to read"
- read csvFile
- file=$csvFile
- if ! [[ $? == 0 ]]
- then
- echo "Failed to read file. Exiting.."
- exit 1
- else
- echo "File successfully read."
- fi
- fi
- IFS=";"
- while read name birthDate group folder
- do
- echo "Column 1 value: $name"
- #Checking if user exists
- id -u $name
- if ! [[ $? == 0 ]]
- then
- echo "User doesn't exists.. Creating user."
- else
- echo "User already exists"
- fi
- #Calculating the default username
- $username=$(echo $name | head -c 1)
- lastname=$(echo $col1 | cut -d '@' -f1)
- lastname=$(echo $lastname | cut -d '.' -f2)
- #Calculating the default pass
- newPass=$(date --date="$birthDate" +%m%Y)
- echo "Your password is: $newPass"
- #Creating user
- sudo useradd -d /home/$name -m -s /bin/bash -p $newPass $name
- if ! [[ $? == 0 ]]
- then
- echo "Creation of user $name failed. Aborting."
- #exit 2 UNCOMMENTME
- else
- echo "Creation of user $name successful."
- fi
- #Setting password to change on login
- sudo chage -d 0 $name
- #Checking if the group exists
- id -g $group
- if ! [[ $? == 0 ]]
- then
- echo "Group doesn't exists.. Creating group."
- else
- echo "Group already exists"
- fi
- #Creating the group
- sudo groupadd $group
- if ! [[ $? == 0 ]]
- then
- echo "Failed to create group."
- else
- echo "Group created succesfully"
- fi
- #Adding user to the group
- sudo useradd -g $group $name
- if ! [[ $? == 0 ]]
- then
- echo "Failed to add user: $col1 to the group"
- else
- echo "$col1 added to group $col3"
- fi
- #Creating group folder
- if [ -d $folder ]; then
- mkdir -p $folder
- fi
- if ! [[ $? == 0 ]]
- then
- echo "Created group folder $folder successfully"
- else
- echo "Group folder $folder failed to create"
- fi
- #Changing file permissions for gorup directory
- chgrp -R $group $folder
- if ! [[ $? == 0 ]]
- then
- echo "$group owns $folder"
- else
- echo "failed to assign $folder to $group"
- fi
- chmod -R 770 $folder
- if ! [[ $? == 0 ]]
- then
- echo "$folder now has full group permissions"
- else
- echo "$folder permissions error"
- fi
- done < $file
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement