Advanced · 30 min · practise every command in the built-in terminal

🛠 Project: New Teammate Script

New people join the web team every month, and each one needs the same setup. Ivana's list is in ~/new-hires.txt. Doing it by hand means five commands per person, and one day someone gets forgotten in the wrong group. Your job: one script that does it all:

sudo ~/scripts/onboard.sh lena

What the script has to do

  1. Without a name, print how to use it (Usage: onboard.sh USERNAME) and stop with an error
  2. Create the account, with a home folder and bash as its shell
  3. Put it into the developers group
  4. Make a projects folder in the new home folder, owned by the new user
  5. Say that it's done

Building blocks

echo "Hello, $1"                       # $1: the first word after the script's name
if [ -z "$1" ]; then echo "no name"; exit 1; fi   # -z: empty; exit 1 stops with an error
useradd -m -s /bin/bash NAME           # Account, home folder (-m), shell (-s)
usermod -aG developers NAME            # Into the group, keeping the others (-a)
mkdir /home/NAME/projects
chown NAME:NAME /home/NAME/projects    # Owner and group

💡 The script runs with sudo, so everything in it runs as root — no sudo needed inside. Test the "no name" case too: ~/scripts/onboard.sh; echo $? shows the exit status.

Practice tasks

  1. Create ~/scripts/onboard.sh, a bash script, and make it executable
  2. Without a name it prints how to use it and stops with an error
  3. Run it for lena: she gets an account with a home folder, in the developers group
  4. lena has a ~/projects folder that belongs to her
  5. Set up tomas with the same script

Commands in this lesson

Projects