Exercise: A Morning Start-Up Script
Write morning.sh, a script that greets whoever runs it and shows the date, the to-do list and the free disk space, make it runnable, turn it into a command called morning, and let a check script run it from another folder to tell you what's done.
Apprentice · Unix · Linux · Updated
Your mornings start with the same small jobs: a hello, the date, your to-do list, and a look at the free disk space before it fills up and surprises you. This exercise turns those jobs into an application of your own: a script, morning.sh, that does all four, and then a command, morning, that runs it by name. A check script runs your command the way a stranger would, from another folder and under another user name, and tells you which parts are done.
Before you start
You need a terminal: the Terminal app on a Mac, or Ubuntu on Linux or in WSL on Windows. The check runs with bats, a small program that runs checks written for bash. If bats --version doesn't print a version number yet, install it:
sudo apt install bats # Ubuntu, and Ubuntu in WSL
brew install bats-core # a Mac, with Homebrew
sudo asks for your password, the one you use to sign in; nothing shows on the screen while you type it. On Windows, keep the exercise inside Ubuntu, in your Linux home folder, not in a Windows folder under /mnt/c: Windows folders don't keep the x that chmod +x adds.
Get the exercise code
Open this lesson's GitHub repository, github.com/ask-codey/exercise-bash-a-morning-start-up-script, also linked from the Exercise box beside the lesson. Click Code, then Download ZIP, and unzip it into your terminal-practice folder as exercise-bash-a-morning-start-up-script. Or clone it with Git.
The folder holds:
morning.sh: your script, with only a few comments in it for now. This is the file you write.bin/: stands in for your~/bin, so the check can see your command. Your copy goes here.todo.txt: a sample to-do list.answers.txt: three questions, each with a numbered line for your answer.check.bats: the check. You don't need to change it.
Your script reads the to-do list from ~/terminal-practice/morning/todo.txt, the one you made in Your First Script. If you don't have it, copy the sample there:
mkdir -p ~/terminal-practice/morning
cp todo.txt ~/terminal-practice/morning/todo.txt
What to do
Open a terminal and cd into the exercise folder (the one with answers.txt in it). Then work through the jobs:
- Write the script. Open
morning.shin nano (nano morning.sh) or in VS Code (code .), delete the comments that are there, and write your own: the shebang on line 1, a comment saying what the script does, and then commands that greet whoever runs it, show today's date, show the to-do list and show the free disk space (Your First Script). - Let it run.
chmod +x morning.sh, then./morning.sh. Read the output: is everything there? - Run it from somewhere else.
cd ~, then run it by its full path. If the list is missing, the path inside the script only works from its own folder (Run It from Anywhere: ~/bin on Your PATH). - Make it a command. Back in the exercise folder, copy the script into
bin/asmorning, without the.sh. Put that folder on your PATH for this terminal withexport PATH="$PWD/bin:$PATH"($PWDholds the folder you're in), thencd /tmpand typemorning. - Answer the three questions in
answers.txt, about naming and permissions.
The file starts like this:
# Example: what is the name of the file your script is written in?
# 0) morning.sh
# 1. You'd like to name a script test. What kind of command is test already? (type test tells you.)
1)
If you use VS Code with ShellCheck, look for underlines as you type, and fix any it shows (Writing Scripts in VS Code).
Check your work
From the exercise folder:
bats check.bats
At first most checks fail. That's expected: each job you finish turns its check into a ✓. A failing check names the job and gives a hint, for example From another folder, the script couldn't find the to-do list. A script runs in the folder you're in, so start the path with ~: cat ~/terminal-practice/morning/todo.txt. To be fair to every computer, the check runs your script with a pretend home folder holding a to-do list of its own, as a user named maya, so your real files are never touched.
Hints
- Greet
$USER, not your own name: the check runs the script asmaya. - Show the list from its file with
cat. Typing the list's lines into the script won't pass: the check's list is different from yours. - Today's date:
date '+Today is %A, %B %d.', or anydatethat shows the day's name. chmod +xbefore you copy:cpkeeps thex, so the copy may run too. Change the script later? Copy it again.- Nothing above the shebang, not even a blank line or a comment.
When you're done
Every check passes:
check.bats
✓ morning.sh starts with the shebang #!/usr/bin/env bash
✓ a comment says what morning.sh does
✓ morning.sh may run
✓ morning.sh runs without an error, from any folder
✓ it greets whoever runs it
✓ it shows today's date
✓ it shows the to-do list in ~/terminal-practice/morning, from any folder
✓ it shows the free disk space
✓ bin/morning is a copy of morning.sh, without the .sh
✓ morning runs by name from any folder, with bin on the PATH
✓ 1) what kind of command test is
✓ 2) morning.sh's permissions
✓ 3) the command that shows whether a name is taken
13 tests, 0 failures
Now make it yours for real: copy morning.sh into your own ~/bin as morning, open a new terminal, and type morning tomorrow when you sit down. What else would you like it to show you each morning?
Submit your exercise
The exercise is how you finish this lesson. When your work is done, put it on GitHub in a repo of your own, then submit that repo's address here.
On GitHub, click +, then New repository. Name it (for example
exercise-bash-a-morning-start-up-script), choose Public, and click Create repository. Leave out the README: your folder already has one.In a terminal in your exercise folder (Ubuntu in WSL on Windows), send your work to your new repo. If you downloaded the ZIP:
git init -b main git add . git commit -m "My solution" git remote add origin https://github.com/your-name/exercise-bash-a-morning-start-up-script.git git push -u origin mainIf you cloned the starter instead, point your copy at your own repo, then commit and push:
git remote set-url origin https://github.com/your-name/exercise-bash-a-morning-start-up-script.git git add . git commit -m "My solution" git push -u origin mainPaste your repo's address, such as
https://github.com/your-name/exercise-bash-a-morning-start-up-script, into Submit your exercise on this page. The site checks that the repo is public and holds your commits, and then the lesson is complete.
Changed something after you submitted? Push it, and submit again: your latest submission is the one that counts.
So what
You've written an application, made it runnable and given it a name that works from any folder: the same steps that turn every script, from a morning check to a whole expense tracker, into a tool you'll use for years.