Bash - User Script
This is a bash script made for an Ubuntu distribution of Linux. It allows you to add users, delete users, change the primary group, change the supplementary group, change the default shell and change the expiration date. It loops through a menu allowing you to choose whichever option is needed, until you to choose to quit.
A video demonstrating the output and the code is shown below:
Source Code
1 #!/bin/bash
2
3 # Mostapha A
4 # CST8102
5 # myscript.sh
6 # This script will provide a menu to add, edit and delete users/groups and execute the provided selections
7
8 # initialize the loop control variable
9 choice=t
10
11 # create a while loop to loop through until the user quits
12 while [ $choice != "Q" ] && [ $choice != "q" ]
13 do
14 # clear the screen when the program starts and after every menu choice
15 clear
16
17 #print the menu options
18 echo -e "Choose one of the following options: \nA Create a user account \nB Delete a user account \nC Change supplementary group for a user account
19 D Change initial group for a user account \nE Change default login shell for a user account \nF Change account expiration date for a user account
20 Q to quit"
21
22 #prompt for input and store in the choice variable
23 read -p "What would you like to do?: " choice
24 clear
25
26 #if else structure to do what each choice indicates
27 if [[ $choice = "a" ]] || [[ $choice = "A" ]]
28 then
29 #read input into variables, add a user and confirm its completed
30 read -p "Enter the username: " username
31 read -p "Enter the home directory as an absolute path: " directory
32 read -p "Enter the default login shell as an absolute path: " loginShell
33 useradd -d "$directory" -m -s "$loginshell" "$username"
34 cat /etc/passwd | grep "$username"
35
36
37 elif [[ $choice = "b" ]] || [[ $choice = "B" ]]
38 then
39 #read input into variables and delete indicated user, confirm its completed
40 read -p "Enter the username of the user to be deleted: " username
41 userdel -r "$username"
42
43
44 elif [[ $choice = "c" ]] || [[ $choice = "C" ]]
45 then
46 #read input into variables, add indicated suplemental group to user and confirm its completed
47 read -p "Enter the username to be added to a group: " username
48 read -p "Enter the supplemental group name: " group
49 usermod -G "$group" "$username"
50 groups "$username"
51
52
53 elif [[ $choice = "d" ]] || [[ $choice = "D" ]]
54 then
55 #read input into variables, change indicated initial group of user and confirm its completed
56 read -p "Enter the username to change the initial group: " username
57 read -p "Enter the initial group name: " group
58 usermod -g "$group" "$username"
59 groups "$username"
60
61
62 elif [[ $choice = "e" ]] || [[ $choice = "E" ]]
63 then
64 #read input into variables, change the indicated shell of the user and confirm its completed
65 read -p "Enter the username for the change of shell: " username
66 read -p "Enter the shell name: " shell
67 chsh -s "$shell" "$username"
68 cat /etc/passwd | grep $username
69
70 elif [[ $choice = "f" ]] || [[ $choice = "F" ]]
71 then
72 #read input into variables, change the indicated users expiry date and confirm it is completed
73 read -p "Enter the username to change the expiry date of: " username
74 read -p "Enter the new expiry date in YYYY-MM-DD format: " expiry
75 usermod -e "$expiry" "$username"
76 chage -l "$username" | grep "Account expires"
77
78 elif [[ $choice = "q" ]] || [[ $choice = "Q" ]]
79 then
80 #if they quit tell them they are
81 echo Quitting...
82 else
83 #if it is not any of the listed options, print an error message
84 echo Invalid input, try again. Select one of the listed options.
85 fi
86
87 #sleep for 3 seconds and clear
88 sleep 3
89 clear
90
91
92 done
93