Advertisement
Guest User

Untitled

a guest
Sep 27th, 2017
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.11 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. file=$1
  4. # Check that the user provided a file as the first argument
  5. if [[ ! -f $1 || -z $1 ]]
  6. then
  7. echo "No argument supplied. Please enter the file you wish to read"
  8. read csvFile
  9. file=$csvFile
  10. if ! [[ $? == 0 ]]
  11. then
  12. echo "Failed to read file. Exiting.."
  13. exit 1
  14. else
  15. echo "File successfully read."
  16. fi
  17. fi
  18.  
  19. IFS=";"
  20. while read name birthDate group folder
  21. do
  22. echo "Column 1 value: $name"
  23.  
  24. #Checking if user exists
  25. id -u $name
  26. if ! [[ $? == 0 ]]
  27. then
  28. echo "User doesn't exists.. Creating user."
  29. else
  30. echo "User already exists"
  31. fi
  32.  
  33. #Calculating the default username
  34. $username=$(echo $name | head -c 1)
  35. lastname=$(echo $col1 | cut -d '@' -f1)
  36. lastname=$(echo $lastname | cut -d '.' -f2)
  37.  
  38. #Calculating the default pass
  39. newPass=$(date --date="$birthDate" +%m%Y)
  40. echo "Your password is: $newPass"
  41.  
  42. #Creating user
  43. sudo useradd -d /home/$name -m -s /bin/bash -p $newPass $name
  44. if ! [[ $? == 0 ]]
  45. then
  46. echo "Creation of user $name failed. Aborting."
  47. #exit 2 UNCOMMENTME
  48. else
  49. echo "Creation of user $name successful."
  50. fi
  51.  
  52. #Setting password to change on login
  53. sudo chage -d 0 $name
  54.  
  55. #Checking if the group exists
  56. id -g $group
  57. if ! [[ $? == 0 ]]
  58. then
  59. echo "Group doesn't exists.. Creating group."
  60. else
  61. echo "Group already exists"
  62. fi
  63.  
  64. #Creating the group
  65. sudo groupadd $group
  66. if ! [[ $? == 0 ]]
  67. then
  68. echo "Failed to create group."
  69. else
  70. echo "Group created succesfully"
  71. fi
  72.  
  73. #Adding user to the group
  74. sudo useradd -g $group $name
  75. if ! [[ $? == 0 ]]
  76. then
  77. echo "Failed to add user: $col1 to the group"
  78. else
  79. echo "$col1 added to group $col3"
  80. fi
  81.  
  82. #Creating group folder
  83. if [ -d $folder ]; then
  84. mkdir -p $folder
  85. fi
  86. if ! [[ $? == 0 ]]
  87. then
  88. echo "Created group folder $folder successfully"
  89. else
  90. echo "Group folder $folder failed to create"
  91. fi
  92.  
  93. #Changing file permissions for gorup directory
  94. chgrp -R $group $folder
  95. if ! [[ $? == 0 ]]
  96. then
  97. echo "$group owns $folder"
  98. else
  99. echo "failed to assign $folder to $group"
  100. fi
  101. chmod -R 770 $folder
  102. if ! [[ $? == 0 ]]
  103. then
  104. echo "$folder now has full group permissions"
  105. else
  106. echo "$folder permissions error"
  107. fi
  108.  
  109. done < $file
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement