A user management script

In this exercise you will write a script that adds and deletes user accounts, plus attends to ancillary details. The useradd program can serve as the centerpiece of such a custom script for adding users. It is, after all, a program that does exactly that. But as a system administrator in your organization, you might want to embellish user account creation by supplementing useradd with some customizations. Perform this exercise in a clone of your provided virtual machine, as root.


Name your script toggle-user.sh.

Include an interpreter declaration in line one "#!/bin/bash"

The user is supposed to give the script a username as command-line argument. Check whether he did (test the number of supplied arguments, making sure it's 1). If not, print out a usage message "You must supply a user name argument." then exit with an exit status of 1.

Main program - is just a 2-way branch if command. The condition for the if is whether or not an account already exists under the name given by the user. If so that account is to be deleted. If not an account by that name is to be created. For those two purposes you will write two functions (below). In the add branch just call your function that adds. In the delete branch first ask the user for a confirmation, "Are you sure you want to delete user jack?" or whatever the name is. This leads to another branch. If the user says yes, go ahead and call your function that deletes. If no, print a confirming message "User jack has not been deleted." When calling either of your functions, you must pass the username for it to create or delete.

Functions - name them add-user and del-user.

In the add-user function use the useradd command to create the new account. Then modify the account to use the Korn shell /bin/ksh instead of the default Bourne Again shell /bin/bash. Modify it to hold "CS41 student" in the comment field of the /etc/passwd record for this user. Set the password to be the username (programatically, not interactively). Add this user account to the "wheel" group. Set the account to require a password change upon first login. Your organization gives free health club membership to users as a benefit. Append the username to a list of health club members named /tmp/healthclub-members.txt . Finally code an if branch where the confirmatory test is whether the account exists. If so print a confirmatory message "User jack has been added" or otherwise "User jack has not been added".

In the del-user function delete the account by using the userdel command. Employ userdel's option to have it remove the user's home directory. Code a single-branch if that tests whether the account does not exist and, if that's so, prints "User jack has been deleted". Then, delete the user's name from the health club members list in /tmp/healthclub-members.txt.

In executing various commands as part of your if conditions, always prevent their output from appearing on the screen (their output isn't why they're here, it's a distraction).


Pseudo code:

main program:

if the account already exists
  ask the user if he really wants to delete
  if so
    call the function that deletes
    print a message that you didn't delete
  call the function that adds

add-user function:

add the username with useradd
change his shell to /bin/ksh
change the comment field in his /etc/passwd user record to "CS41 student"
set the password to the user's name              
(bad security practice)
add the username to the "wheel" group
set the account to require a password change upon initial login
enter the username into /tmp/healthclub-members.txt
if the account exists
  tell the user "User ... has been added"
 tell the user "User ... has not been added"

del-user function:

delete the username with userdel
if the account does not exist
  tell the user "User ... has been deleted"
remove the username from /tmp/healthclub-members.txt

Programming tips:

To find out if a user exists there's 1) a command you could run, or 2) a place in which you could search.

When you pass a username to a function, if positional parameters are involved remember that those of a function are a separate bunch from those of the program though they bear the same names ($1, $2, etc    ).

Setting the password programatically could be done with via certain usages of the passwd or chpasswd command. If you express a password by printing it, use the printf command. Or if you use the echo command include its -n option so it won't tack an extra character on the end (printf abc prints 3 characters, echo abc prints 4, which is not what you think).

You can remove a line from a text file using the sed command (stream editor). It will search for lines containing a specified substring and then act on such lines. For deleting lines that contain "jack"

sed  -i  /jack/d  <name-of-file>

You can force a pasword reset upon next login via usages of the passwd or chage command.

Command output is banished by redirecting it to /dev/null. remember programs have two "outlets" for printing, stdout and stderr. The redirection operator for stdout is well known. It's ">". But if any command you use prints to stderr you'll need to use the redirection operator variant that captures that, or the one that captures both.


If you are working on something that's supposed to affect what happens upon login and wish to test it (e.g. you want to know if your passwords are working), you need to go to a login prompt. You can do that without disturbing your working environment if you use virtual terminals. The alt-Fn keystroke combinations take you from one virtual terminal to another. n is a digit 1 through 6. Assuming you are working in virtual terminal 1 (likely) press alt-F2. When you want to go back, press alt-F1.

To turn in:

Transfer your toggle-user.sh script file to your assignments directory. To grade it, I will run it in my virtual machine and look for the artifacts it is supposed to produce and behavior it is supposed to have.