TT Lab
Get started
Learn Learning paths Courses

Shell Scripting

Writing Your First Shell Script

Continue in TT Lab

Goal

You will go around the skeleton of a shell script once: shebang, execute permission, arguments, environment variables, conditional branches, and exit codes.

Why it matters

Scripts are called not by people but by other programs. So the output streams and the exit code are the interface. Results go to stdout, errors and usage go to stderr, and success or failure goes in the exit code. Only when you keep this convention does a usage such as result=$(script.sh) work and can CI detect failures. The moment you send the usage message to stdout, it becomes the result value. And passing configuration through environment variables and running with a reasonable default when they are absent is the standard form in deployment automation.

Steps

  1. Create /root/bin/greet.sh and make its first line #!/bin/bash.
  2. Give that file execute permission.
  3. Make greet.sh labhub print exactly Hello, labhub!.
  4. Add a default so that running it without arguments gives Hello, world!. If an argument is given, that argument must still be used.
  5. If the environment variable GREETING is set, change the greeting to its value. GREETING=Hi greet.sh labhub must give Hi, labhub!, with no setting it must be Hello, and GREETING=Yo greet.sh must give Yo, world!.
  6. Create /root/bin/checkfile.sh <경로> (the argument is a path). It prints file for a regular file, dir for a directory, and missing if it does not exist.
  7. Create /root/bin/grade.sh <점수> (the argument is a score). 90 or above is A, 80–89 is B, 70–79 is C, and anything below that is F.
  8. Create /root/bin/deploy.sh. It reads the environment variable APP_ENV and:
    • if it is not set, prints a message to standard error and exits with exit code 2
    • if the value is neither prod nor stage, exits with exit code 3
    • if it is prod or stage, prints deploying to <값> (the value goes in place of the placeholder) and exits with exit code 0

Notes

Script file and shebang

Create /root/bin/greet.sh and make its first line #!/bin/bash.

The #! at the start of the first line is a marker that tells the kernel which interpreter to use to run this file.

Give it execute permission

Give that file execute permission.

Turn on the execute bit with chmod +x.

Take an argument and print it

Make greet.sh labhub print exactly Hello, labhub!.

The first positional argument is $1. You must match the exact placement of the comma and the exclamation mark.

Default value for an argument

Add a default so that running it without arguments gives Hello, world!. If an argument is given, that argument must still be used.

With the ${1:-default} syntax, the default is used only when there is no argument. If there is an argument, it must stay as it is.

Change the greeting with an environment variable

If the environment variable GREETING is set, change the greeting to its value. GREETING=Hi greet.sh labhub must give Hi, labhub!, with no setting it must be Hello, and GREETING=Yo greet.sh must give Yo, world!.

Both the environment variable and the positional argument need default handling. They must not interfere with each other.

Identify the file type

Create /root/bin/checkfile.sh <경로> (the argument is a path). It prints file for a regular file, dir for a directory, and missing if it does not exist.

-f and -d test for a regular file and a directory respectively. If you get the order wrong, a directory may fall through to missing.

Assign a grade to a score

Create /root/bin/grade.sh <점수> (the argument is a score). 90 or above is A, 80–89 is B, 70–79 is C, and anything below that is F.

The boundary values are the trap. Check 90 and 89, 80 and 79, and 70 and 69 by hand.

Environment variable validation and exit codes

Create /root/bin/deploy.sh. It reads the environment variable APP_ENV and:

You must distinguish standard output from standard error and return a different exit code for each situation. The number after exit is the convention.