Linux on call · Chapter 24 of 30
Your first script
By the end of this chapter you can write a bash script that runs like any other command, takes the words you give it, and keeps them intact. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Shell scripting
- 4 tasks
- Linux
- In the all labs plan
The first 2 chapters of Linux on call are free. This one comes with the all labs plan.
Why you need this
Kabir wants a greeting on the counter screen every morning, for whoever opens the shop: his note with the exact lines is in NOTE.txt. Bunty has already written greet.sh:
cat greet.sh
./greet.sh Ravi
bash greet.sh Ravi
shop=Tapri
echo Namaste, Bunty! Welcome to $shop.
bash: ./greet.sh: Permission denied
Namaste, Bunty! Welcome to Tapri.
It cannot be run as a command, and when it runs, it greets Bunty whatever name you give it. Typing the commands by hand every morning is not a script either: the counter screen needs one command that takes the name.
What you build: 4 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
Make greet.sh a real script that runs as ./greet.sh
Make greet.sh a real script, so you can run it as ./greet.sh.
1. Write greet.sh again with a bash shebang as its very first line: #!/bin/bash (or #!/usr/bin/env bash). printf '%s\n' writes one line for each argument.
2. Give the file the execute permission for you, with chmod.
3. Run ./greet.sh Ravi to see that it runs.
To pass: the first line of greet.sh is a bash shebang, and greet.sh has the execute permission for you.
- Task 2
Greet the name you give the script
Make the script greet the name you give it, not always Bunty.
1. In greet.sh, put the script's first argument where Bunty's name is.
2. Keep the rest of the line exactly the same: the comma, the exclamation mark and the full stop.
3. Run ./greet.sh Ravi. It prints: Namaste, Ravi! Welcome to Tapri.
4. Run ./greet.sh Anjali. It greets Anjali the same way.
To pass: the first line that ./greet.sh NAME prints is exactly Namaste, NAME! Welcome to Tapri. with the name you gave it.
- Task 3
Keep every space in a name with spaces
A name with spaces must come out exactly as you give it, with every space.
1. Run ./greet.sh "Bunty the Intern" (three spaces each time). See if the spaces are lost.
2. In greet.sh, put double quotes around the text that uses the name.
3. Run it again. All six spaces are kept.
To pass: ./greet.sh "Meera Rao" prints Namaste, Meera Rao! Welcome to Tapri. as its first line, and ./greet.sh "Bunty the Intern" keeps all six spaces in its first line.
- Task 4
Add a second line: Names given: N
Make the script also print how many names it was given.
1. Under the greeting line in greet.sh, add a line that prints Names given: and then the number of arguments.
2. Use $#. It is the number of arguments the script was given.
3. Try ./greet.sh Ravi Kabir Dev. It still greets only Ravi, and then prints Names given: 3.
To pass: the second line that the script prints is exactly Names given: N, where N is the number of arguments. A quoted name with spaces counts as one.
What you will learn
- A real script: the shebang line, the execute bit, and ./ in front.
- Arguments: $1, $2 and $#, the words a script is given.
- Quoting: why "$1" keeps a name with spaces whole.
- Writing the file without an editor: printf, one line per argument.
More in Shell scripting
Build "Your first script" yourself
Tapri’s ordering app moves onto a Linux server, and the server is now yours to run. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of Linux on call are free. This one comes with the all labs plan.