TT Lab
Blog 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), and at the end you tell apart by cause and fix four scripts that "have the execute bit but do not run".

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.

When you call a script by its path, it is the kernel, not the shell, that reads the shebang and finds the interpreter. So "it has the execute bit" and "it runs" are different facts, and many production incidents where a script does not run are cases where different causes (line-ending characters, the interpreter path, per-account execute bits, directory permissions) hide behind the same error message. The final incident has you go through all four at once.

Steps

  1. Create /root/bin/greet.sh and make its first line #!/bin/bash.
  2. Give that file execute permission, and make it print something and exit with 0 when called by its path alone, without putting bash in front.
  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/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.
  7. Create /root/bin/deploy.sh. If the environment variable APP_ENV is not set, it prints a message to standard error and exits with exit code 2; if the value is neither prod nor stage, it exits with exit code 3; if it is prod or stage, it prints deploying to <값> (the value goes in place of the placeholder) and exits with exit code 0.
  8. Incident incident sh-cant-exec: four nightly jobs of the batch account do not run even though they have the execute bit. Tell apart the cause for each job, fix it so it runs as batch, and leave the cause code and evidence in the report.

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 and run it by its path

Give that file execute permission, and make it print something and exit with 0 when called by its path alone (/root/bin/greet.sh), without putting bash in front. The grader has the kernel execute it directly without going through a shell; if the shebang is broken or the file is empty, it fails even with the execute bit set.

The execute bit is the x position of the file mode, and it exists separately for owner, group, and others. After turning on the bit, call the file by its path alone without putting an interpreter in front; only then does the kernel look for the interpreter that the first line points to. The shell sometimes reads files of unknown format on the kernel's behalf, so "it works" in the shell alone does not confirm it.

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.

Parameter expansion has a form that attaches "a value to use in place only when it is empty" (in the reading's table, the one that leaves no value in the variable). 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.

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.

Incident: the execute bit is there, but four nightly jobs do not run

Running incident sh-cant-exec raises the incident. Four nightly jobs that run as the batch account (/srv/jobs/bin/rotate.sh, report.sh, notify.sh, and /srv/jobs/private/cleanup.sh) all fail even though ls -l shows an x on them (rerun with /srv/jobs/run-all, log in /srv/jobs/run.log). Find the cause for each job and fix it so all four run as batch. Do not change what the scripts do (the body from the second line on), do not open them so anyone can write, and /srv/jobs/private/notify.key must stay unreadable by batch. In /root/incident/sh-cant-exec/report.txt, write four lines, rotate: <코드>, report: <코드>, notify: <코드>, and cleanup: <코드>, plus evidence: <관찰한 출력 한 줄> (one line of output you observed). Each job's code is one of the following: crlf (CRLF line endings), bad-interpreter (the interpreter the shebang points to does not exist), no-exec-bit (no x in that account's position), dir-search (no x to pass through a directory on the path), noexec-mount (a filesystem mounted with noexec).

Direction: there are two error messages (No such file, Permission denied) but four jobs, so do not assume one cause per message. root is not blocked by permission bits, so reproduce as batch. Tools: runuser -u batch --, head, cat -A, ls -l, namei -l, stat. Concretely: if you get "No such file" when the file clearly exists, what the kernel could not find is the interpreter that the first line (the shebang) points to. Reveal that line down to its invisible characters, and check whether a file really exists at that path. For "Permission denied", check separately whether the file's x is in batch's position (owner, group, others) and whether every directory on the path has x. A directory's x and r are different permissions.